@trenskow/api-error 2.2.12 → 2.3.0

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 +37 -0
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -241,6 +241,42 @@ 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
+ toJSON(options = {}) {
273
+ return merge(super.toJSON(options), {
274
+ errors: this._errors.map((error) => error.toJSON(options))
275
+ });
276
+ }
277
+
278
+ }
279
+
244
280
  module.exports = exports = ApiError;
245
281
  exports.NotAuthorized = NotAuthorized;
246
282
  exports.Forbidden = Forbidden;
@@ -253,3 +289,4 @@ exports.PayloadTooLarge = PayloadTooLarge;
253
289
  exports.InternalError = InternalError;
254
290
  exports.NotImplemented = NotImplemented;
255
291
  exports.ServiceUnavailable = ServiceUnavailable;
292
+ 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.0",
4
4
  "description": "A small collection of error objects that can be used for REST APIs.",
5
5
  "main": "index.js",
6
6
  "scripts": {