@trenskow/api-error 2.2.11 → 2.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +41 -0
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -241,6 +241,46 @@ class ServiceUnavailable extends ApiError {
241
241
 
242
242
  }
243
243
 
244
+ class Aggregated extends ApiError {
245
+
246
+ constructor(message, options) {
247
+
248
+ [ message, options ] = ApiError._correctArguments(message, options);
249
+
250
+ super(message || 'Multiple errors occurred.', merge(options, {
251
+ name: options.name || 'aggregated',
252
+ statusCode: 400
253
+ }));
254
+
255
+ this._errors = options.errors || [];
256
+
257
+ if (!Array.isArray(this._errors)) this._errors = [this._errors];
258
+
259
+ this._errors = this._errors.map((error) => this._check(error));
260
+
261
+ }
262
+
263
+ _check(error) {
264
+ if (error instanceof ApiError) return error;
265
+ throw new TypeError('Error must be of type `ApiError`.');
266
+ }
267
+
268
+ add(error) {
269
+ this._errors.push(this._check(error));
270
+ }
271
+
272
+ get errors() {
273
+ return this._errors;
274
+ }
275
+
276
+ toJSON(options = {}) {
277
+ return merge(super.toJSON(options), {
278
+ errors: this._errors.map((error) => error.toJSON(options))
279
+ });
280
+ }
281
+
282
+ }
283
+
244
284
  module.exports = exports = ApiError;
245
285
  exports.NotAuthorized = NotAuthorized;
246
286
  exports.Forbidden = Forbidden;
@@ -253,3 +293,4 @@ exports.PayloadTooLarge = PayloadTooLarge;
253
293
  exports.InternalError = InternalError;
254
294
  exports.NotImplemented = NotImplemented;
255
295
  exports.ServiceUnavailable = ServiceUnavailable;
296
+ exports.Aggregated = Aggregated;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trenskow/api-error",
3
- "version": "2.2.11",
3
+ "version": "2.3.1",
4
4
  "description": "A small collection of error objects that can be used for REST APIs.",
5
5
  "main": "index.js",
6
6
  "scripts": {