@trenskow/api-error 2.4.7 → 2.4.8
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.
- package/.eslintrc.js +4 -1
- package/index.js +29 -7
- package/package.json +1 -1
package/.eslintrc.js
CHANGED
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
|
-
|
|
29
|
+
|
|
30
|
+
let options = merge(data, {
|
|
11
31
|
message: data.message,
|
|
12
32
|
statusCode: statusCode,
|
|
13
|
-
origin: origin
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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,7 +288,7 @@ class Aggregated extends ApiError {
|
|
|
266
288
|
statusCode: 400
|
|
267
289
|
}));
|
|
268
290
|
|
|
269
|
-
this._errors = options.errors || [];
|
|
291
|
+
this._errors = options.errors || options.underlying?.errors || [];
|
|
270
292
|
|
|
271
293
|
this._errors = this._check(this._errors);
|
|
272
294
|
|