@trenskow/api-error 2.4.7 → 2.4.9

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 (3) hide show
  1. package/.eslintrc.js +4 -1
  2. package/index.js +41 -11
  3. package/package.json +1 -1
package/.eslintrc.js CHANGED
@@ -11,7 +11,10 @@ module.exports = {
11
11
  'rules': {
12
12
  'indent': [
13
13
  'error',
14
- 'tab'
14
+ 'tab',
15
+ {
16
+ 'SwitchCase': 1
17
+ }
15
18
  ],
16
19
  'linebreak-style': [
17
20
  'error',
package/index.js CHANGED
@@ -6,15 +6,37 @@ const
6
6
 
7
7
  class ApiError extends Error {
8
8
 
9
+ static _type(name) {
10
+ switch (name) {
11
+ case 'not-authorized': return NotAuthorized;
12
+ case 'payment-required': return PaymentRequired;
13
+ case 'forbidden': return Forbidden;
14
+ case 'not-found': return NotFound;
15
+ case 'already-exists': return Conflict;
16
+ case 'method-not-allowed': return MethodNotAllowed;
17
+ case 'bad-request': return BadRequest;
18
+ case 'too-many-requests': return TooManyRequests;
19
+ case 'payload-too-large': return PayloadTooLarge;
20
+ case 'internal-error': return InternalError;
21
+ case 'not-implemented': return NotImplemented;
22
+ case 'service-unavailable': return ServiceUnavailable;
23
+ case 'aggregated': return Aggregated;
24
+ default: return ApiError;
25
+ }
26
+ }
27
+
9
28
  static parse(data, statusCode, origin) {
10
- let error = new ApiError(merge(true, data, {
29
+
30
+ let options = merge(data, {
11
31
  message: data.message,
12
32
  statusCode: statusCode,
13
- origin: origin,
14
- keyPath: data.keyPath,
15
- errors: data.errors?.map((error) => ApiError.parse(error, statusCode, origin))
16
- }));
17
- return error;
33
+ origin: origin
34
+ });
35
+
36
+ options.errors = options.errors?.map((error) => ApiError.parse(error, statusCode, origin));
37
+
38
+ return new (this._type(data.name || 'bad-request'))(options);
39
+
18
40
  }
19
41
 
20
42
  static _correctArguments(message, options) {
@@ -266,19 +288,27 @@ class Aggregated extends ApiError {
266
288
  statusCode: 400
267
289
  }));
268
290
 
269
- this._errors = options.errors || [];
270
-
271
- this._errors = this._check(this._errors);
291
+ this._errors = options.underlying?.errors || options.errors || [];
272
292
 
273
293
  }
274
294
 
275
295
  _check(errors) {
276
- if (Array.isArray(errors) && errors.every((error) => error instanceof ApiError)) return errors;
296
+
297
+ if (Array.isArray(errors) && errors.every((error) => {
298
+ let prototype = Object.getPrototypeOf(error);
299
+ while (prototype !== null) {
300
+ if (prototype.constructor.name === 'ApiError') return true;
301
+ prototype = Object.getPrototypeOf(prototype);
302
+ }
303
+ return false;
304
+ })) return errors;
305
+
277
306
  throw new TypeError('Error must an array of type `ApiError`.');
307
+
278
308
  }
279
309
 
280
310
  add(error) {
281
- this._errors = this._errors.concat(this._check(error));
311
+ this._errors = this._errors.concat(this._check(Array.isArray(error) ? error : [error]));
282
312
  }
283
313
 
284
314
  get errors() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trenskow/api-error",
3
- "version": "2.4.7",
3
+ "version": "2.4.9",
4
4
  "description": "A small collection of error objects that can be used for REST APIs.",
5
5
  "main": "index.js",
6
6
  "scripts": {