@trenskow/api-error 2.4.6 → 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 +5 -2
- package/index.js +28 -4
- package/package.json +1 -1
package/.eslintrc.js
CHANGED
|
@@ -5,13 +5,16 @@ module.exports = {
|
|
|
5
5
|
'mocha': true
|
|
6
6
|
},
|
|
7
7
|
'parserOptions': {
|
|
8
|
-
'ecmaVersion':
|
|
8
|
+
'ecmaVersion': 'latest'
|
|
9
9
|
},
|
|
10
10
|
'extends': 'eslint:recommended',
|
|
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,13 +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
33
|
origin: origin
|
|
14
|
-
})
|
|
15
|
-
|
|
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
|
+
|
|
16
40
|
}
|
|
17
41
|
|
|
18
42
|
static _correctArguments(message, options) {
|
|
@@ -264,7 +288,7 @@ class Aggregated extends ApiError {
|
|
|
264
288
|
statusCode: 400
|
|
265
289
|
}));
|
|
266
290
|
|
|
267
|
-
this._errors = options.errors || [];
|
|
291
|
+
this._errors = options.errors || options.underlying?.errors || [];
|
|
268
292
|
|
|
269
293
|
this._errors = this._check(this._errors);
|
|
270
294
|
|