@trenskow/api-error 2.2.12 → 2.3.2

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 +43 -0
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -241,6 +241,48 @@ 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
+ this._errors = this._check(this._errors);
258
+
259
+ }
260
+
261
+ _check(errors) {
262
+ if (Array.isArray(errors) && errors.every((error) => error instanceof ApiError)) return errors;
263
+ throw new TypeError('Error must an array of type `ApiError`.');
264
+ }
265
+
266
+ add(error) {
267
+ this._errors = this._errors.concat(this._check(error));
268
+ }
269
+
270
+ get errors() {
271
+ return this._errors;
272
+ }
273
+
274
+ set errors(errors) {
275
+ this._errors = this._check(errors);
276
+ }
277
+
278
+ toJSON(options = {}) {
279
+ return merge(super.toJSON(options), {
280
+ errors: this._errors.map((error) => error.toJSON(options))
281
+ });
282
+ }
283
+
284
+ }
285
+
244
286
  module.exports = exports = ApiError;
245
287
  exports.NotAuthorized = NotAuthorized;
246
288
  exports.Forbidden = Forbidden;
@@ -253,3 +295,4 @@ exports.PayloadTooLarge = PayloadTooLarge;
253
295
  exports.InternalError = InternalError;
254
296
  exports.NotImplemented = NotImplemented;
255
297
  exports.ServiceUnavailable = ServiceUnavailable;
298
+ exports.Aggregated = Aggregated;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trenskow/api-error",
3
- "version": "2.2.12",
3
+ "version": "2.3.2",
4
4
  "description": "A small collection of error objects that can be used for REST APIs.",
5
5
  "main": "index.js",
6
6
  "scripts": {