yerror 8.0.0 → 9.1.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.
- package/CHANGELOG.md +9 -0
- package/README.md +60 -16
- package/dist/index.d.ts +38 -17
- package/dist/index.js +78 -50
- package/dist/index.js.map +1 -1
- package/dist/index.test.js +108 -84
- package/dist/index.test.js.map +1 -1
- package/package.json +43 -154
- package/src/index.test.ts +144 -143
- package/src/index.ts +98 -71
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
# [9.1.0](https://github.com/nfroidure/yerror/compare/v9.0.0...v9.1.0) (2026-03-28)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Features
|
|
5
|
+
|
|
6
|
+
* add more casting features ([1153fcf](https://github.com/nfroidure/yerror/commit/1153fcf481d7660f247e65f5a8c8596f2abe511c))
|
|
7
|
+
|
|
8
|
+
# [9.0.0](https://github.com/nfroidure/yerror/compare/v8.0.0...v9.0.0) (2026-03-25)
|
|
9
|
+
|
|
1
10
|
# [8.0.0](https://github.com/nfroidure/yerror/compare/v7.0.0...v8.0.0) (2023-08-12)
|
|
2
11
|
|
|
3
12
|
|
package/README.md
CHANGED
|
@@ -8,7 +8,6 @@
|
|
|
8
8
|
> It helps to know why you got an error.
|
|
9
9
|
|
|
10
10
|
[](https://github.com/nfroidure/yerror/blob/main/LICENSE)
|
|
11
|
-
[](https://coveralls.io/github/git://github.com/nfroidure/yerror.git?branch=main)
|
|
12
11
|
|
|
13
12
|
|
|
14
13
|
[//]: # (::contents:start)
|
|
@@ -79,7 +78,7 @@ doSomethingAsync('nuts', 'code')
|
|
|
79
78
|
|
|
80
79
|
<dl>
|
|
81
80
|
<dt><a href="#YError">YError</a> ⇐ <code>Error</code></dt>
|
|
82
|
-
<dd><p>A YError class able to contain some
|
|
81
|
+
<dd><p>A YError class able to contain some debugValues and
|
|
83
82
|
print better stack traces</p>
|
|
84
83
|
</dd>
|
|
85
84
|
</dl>
|
|
@@ -88,30 +87,49 @@ doSomethingAsync('nuts', 'code')
|
|
|
88
87
|
|
|
89
88
|
<dl>
|
|
90
89
|
<dt><a href="#printStackTrace">printStackTrace(err)</a> ⇒ <code>string</code></dt>
|
|
91
|
-
<dd><p>Allow to print a stack from anything (especially
|
|
90
|
+
<dd><p>Allow to print a stack from anything (especially caught
|
|
92
91
|
errors that may or may not contain errors 🤷).</p>
|
|
93
92
|
</dd>
|
|
93
|
+
<dt><a href="#hasYErrorCode">hasYErrorCode(err, code)</a> ⇒ <code>boolean</code></dt>
|
|
94
|
+
<dd><p>Allow to check a YError code and cast the error.</p>
|
|
95
|
+
</dd>
|
|
96
|
+
<dt><a href="#pickYErrorWithCode">pickYErrorWithCode(err, code)</a> ⇒ <code>boolean</code></dt>
|
|
97
|
+
<dd><p>Allow to check all errors for a YError code and return the casted the error.</p>
|
|
98
|
+
</dd>
|
|
94
99
|
</dl>
|
|
95
100
|
|
|
96
101
|
<a name="YError"></a>
|
|
97
102
|
|
|
98
103
|
## YError ⇐ <code>Error</code>
|
|
99
|
-
A YError class able to contain some
|
|
104
|
+
A YError class able to contain some debugValues and
|
|
100
105
|
print better stack traces
|
|
101
106
|
|
|
102
107
|
**Kind**: global class
|
|
103
108
|
**Extends**: <code>Error</code>
|
|
104
109
|
|
|
105
110
|
* [YError](#YError) ⇐ <code>Error</code>
|
|
106
|
-
* [
|
|
107
|
-
* [.
|
|
108
|
-
* [.
|
|
111
|
+
* [new YError([errorCode], [debugValues])](#new_YError_new)
|
|
112
|
+
* [.wrap(err, [errorCode], [debugValues])](#YError.wrap) ⇒ [<code>YError</code>](#YError)
|
|
113
|
+
* [.cast(err, [errorCode], [debugValues])](#YError.cast) ⇒ [<code>YError</code>](#YError)
|
|
114
|
+
* [.bump(err, [errorCode], [debugValues])](#YError.bump) ⇒ [<code>YError</code>](#YError)
|
|
115
|
+
|
|
116
|
+
<a name="new_YError_new"></a>
|
|
117
|
+
|
|
118
|
+
### new YError([errorCode], [debugValues])
|
|
119
|
+
Creates a new YError with an error code
|
|
120
|
+
and some debugValues as debug values.
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
| Param | Type | Default | Description |
|
|
124
|
+
| --- | --- | --- | --- |
|
|
125
|
+
| [errorCode] | <code>string</code> | <code>"'E_UNEXPECTED'"</code> | The error code corresponding to the actual error |
|
|
126
|
+
| [debugValues] | <code>any</code> | | Some additional debugging values |
|
|
109
127
|
|
|
110
128
|
<a name="YError.wrap"></a>
|
|
111
129
|
|
|
112
|
-
### YError.wrap(err, [errorCode], [
|
|
130
|
+
### YError.wrap(err, [errorCode], [debugValues]) ⇒ [<code>YError</code>](#YError)
|
|
113
131
|
Wraps any error and output a YError with an error
|
|
114
|
-
code and some
|
|
132
|
+
code and some debugValues as debug values.
|
|
115
133
|
|
|
116
134
|
**Kind**: static method of [<code>YError</code>](#YError)
|
|
117
135
|
**Returns**: [<code>YError</code>](#YError) - The wrapped error
|
|
@@ -120,13 +138,13 @@ Wraps any error and output a YError with an error
|
|
|
120
138
|
| --- | --- | --- | --- |
|
|
121
139
|
| err | <code>Error</code> | | The error to wrap |
|
|
122
140
|
| [errorCode] | <code>string</code> | <code>"'E_UNEXPECTED'"</code> | The error code corresponding to the actual error |
|
|
123
|
-
| [
|
|
141
|
+
| [debugValues] | <code>any</code> | | Some additional debugging values |
|
|
124
142
|
|
|
125
143
|
<a name="YError.cast"></a>
|
|
126
144
|
|
|
127
|
-
### YError.cast(err, [errorCode], [
|
|
145
|
+
### YError.cast(err, [errorCode], [debugValues]) ⇒ [<code>YError</code>](#YError)
|
|
128
146
|
Return a YError as is or wraps any other error and output
|
|
129
|
-
a YError with a code and some
|
|
147
|
+
a YError with a code and some debugValues as debug values.
|
|
130
148
|
|
|
131
149
|
**Kind**: static method of [<code>YError</code>](#YError)
|
|
132
150
|
**Returns**: [<code>YError</code>](#YError) - The wrapped error
|
|
@@ -135,11 +153,11 @@ Return a YError as is or wraps any other error and output
|
|
|
135
153
|
| --- | --- | --- | --- |
|
|
136
154
|
| err | <code>Error</code> | | The error to cast |
|
|
137
155
|
| [errorCode] | <code>string</code> | <code>"'E_UNEXPECTED'"</code> | The error code corresponding to the actual error |
|
|
138
|
-
| [
|
|
156
|
+
| [debugValues] | <code>any</code> | | Some additional debugging values |
|
|
139
157
|
|
|
140
158
|
<a name="YError.bump"></a>
|
|
141
159
|
|
|
142
|
-
### YError.bump(err, [errorCode], [
|
|
160
|
+
### YError.bump(err, [errorCode], [debugValues]) ⇒ [<code>YError</code>](#YError)
|
|
143
161
|
Same than `YError.wrap()` but preserves the code
|
|
144
162
|
and the debug values of the error if it is
|
|
145
163
|
already an instance of the YError constructor.
|
|
@@ -151,12 +169,12 @@ Same than `YError.wrap()` but preserves the code
|
|
|
151
169
|
| --- | --- | --- | --- |
|
|
152
170
|
| err | <code>Error</code> | | The error to bump |
|
|
153
171
|
| [errorCode] | <code>string</code> | <code>"'E_UNEXPECTED'"</code> | The error code corresponding to the actual error |
|
|
154
|
-
| [
|
|
172
|
+
| [debugValues] | <code>any</code> | | Some additional debugging values |
|
|
155
173
|
|
|
156
174
|
<a name="printStackTrace"></a>
|
|
157
175
|
|
|
158
176
|
## printStackTrace(err) ⇒ <code>string</code>
|
|
159
|
-
Allow to print a stack from anything (especially
|
|
177
|
+
Allow to print a stack from anything (especially caught
|
|
160
178
|
errors that may or may not contain errors 🤷).
|
|
161
179
|
|
|
162
180
|
**Kind**: global function
|
|
@@ -166,6 +184,32 @@ Allow to print a stack from anything (especially catched
|
|
|
166
184
|
| --- | --- | --- |
|
|
167
185
|
| err | <code>Error</code> | The error to print |
|
|
168
186
|
|
|
187
|
+
<a name="hasYErrorCode"></a>
|
|
188
|
+
|
|
189
|
+
## hasYErrorCode(err, code) ⇒ <code>boolean</code>
|
|
190
|
+
Allow to check a YError code and cast the error.
|
|
191
|
+
|
|
192
|
+
**Kind**: global function
|
|
193
|
+
**Returns**: <code>boolean</code> - The result
|
|
194
|
+
|
|
195
|
+
| Param | Type | Description |
|
|
196
|
+
| --- | --- | --- |
|
|
197
|
+
| err | <code>Error</code> | The error to cast |
|
|
198
|
+
| code | <code>Error</code> | The code to check |
|
|
199
|
+
|
|
200
|
+
<a name="pickYErrorWithCode"></a>
|
|
201
|
+
|
|
202
|
+
## pickYErrorWithCode(err, code) ⇒ <code>boolean</code>
|
|
203
|
+
Allow to check all errors for a YError code and return the casted the error.
|
|
204
|
+
|
|
205
|
+
**Kind**: global function
|
|
206
|
+
**Returns**: <code>boolean</code> - The result
|
|
207
|
+
|
|
208
|
+
| Param | Type | Description |
|
|
209
|
+
| --- | --- | --- |
|
|
210
|
+
| err | <code>Error</code> | The error to cast |
|
|
211
|
+
| code | <code>Error</code> | The code to check |
|
|
212
|
+
|
|
169
213
|
|
|
170
214
|
# Authors
|
|
171
215
|
- [Nicolas Froidure (formerly at SimpliField)](http://insertafter.com/en/index.html)
|
package/dist/index.d.ts
CHANGED
|
@@ -1,49 +1,48 @@
|
|
|
1
|
-
type
|
|
1
|
+
export type YErrorDebugValue = unknown[];
|
|
2
2
|
/**
|
|
3
|
-
* A YError class able to contain some
|
|
3
|
+
* A YError class able to contain some debugValues and
|
|
4
4
|
* print better stack traces
|
|
5
5
|
* @extends Error
|
|
6
6
|
*/
|
|
7
|
-
declare class YError extends Error {
|
|
7
|
+
declare class YError<T extends unknown[] = YErrorDebugValue> extends Error {
|
|
8
8
|
code: string;
|
|
9
|
-
|
|
9
|
+
debugValues: T;
|
|
10
10
|
wrappedErrors: (Error | YError)[];
|
|
11
11
|
/**
|
|
12
12
|
* Creates a new YError with an error code
|
|
13
|
-
* and some
|
|
13
|
+
* and some debugValues as debug values.
|
|
14
14
|
* @param {string} [errorCode = 'E_UNEXPECTED']
|
|
15
15
|
* The error code corresponding to the actual error
|
|
16
|
-
* @param {
|
|
16
|
+
* @param {any} [debugValues]
|
|
17
17
|
* Some additional debugging values
|
|
18
18
|
*/
|
|
19
|
-
constructor(errorCode
|
|
20
|
-
constructor(wrappedErrors?: Error[], errorCode?: string, ...params: YErrorParams[]);
|
|
19
|
+
constructor(errorCode?: string, debugValues?: T, wrappedErrors?: (Error | YError)[]);
|
|
21
20
|
/**
|
|
22
21
|
* Wraps any error and output a YError with an error
|
|
23
|
-
* code and some
|
|
22
|
+
* code and some debugValues as debug values.
|
|
24
23
|
* @param {Error} err
|
|
25
24
|
* The error to wrap
|
|
26
25
|
* @param {string} [errorCode = 'E_UNEXPECTED']
|
|
27
26
|
* The error code corresponding to the actual error
|
|
28
|
-
* @param {
|
|
27
|
+
* @param {any} [debugValues]
|
|
29
28
|
* Some additional debugging values
|
|
30
29
|
* @return {YError}
|
|
31
30
|
* The wrapped error
|
|
32
31
|
*/
|
|
33
|
-
static wrap<
|
|
32
|
+
static wrap<T extends unknown[] = YErrorDebugValue>(err: Error | YError, errorCode?: string, debugValues?: T): YError;
|
|
34
33
|
/**
|
|
35
34
|
* Return a YError as is or wraps any other error and output
|
|
36
|
-
* a YError with a code and some
|
|
35
|
+
* a YError with a code and some debugValues as debug values.
|
|
37
36
|
* @param {Error} err
|
|
38
37
|
* The error to cast
|
|
39
38
|
* @param {string} [errorCode = 'E_UNEXPECTED']
|
|
40
39
|
* The error code corresponding to the actual error
|
|
41
|
-
* @param {
|
|
40
|
+
* @param {any} [debugValues]
|
|
42
41
|
* Some additional debugging values
|
|
43
42
|
* @return {YError}
|
|
44
43
|
* The wrapped error
|
|
45
44
|
*/
|
|
46
|
-
static cast<
|
|
45
|
+
static cast<T extends unknown[] = YErrorDebugValue>(err: Error | YError, errorCode?: string, debugValues?: T): YError;
|
|
47
46
|
/**
|
|
48
47
|
* Same than `YError.wrap()` but preserves the code
|
|
49
48
|
* and the debug values of the error if it is
|
|
@@ -52,16 +51,16 @@ declare class YError extends Error {
|
|
|
52
51
|
* The error to bump
|
|
53
52
|
* @param {string} [errorCode = 'E_UNEXPECTED']
|
|
54
53
|
* The error code corresponding to the actual error
|
|
55
|
-
* @param {
|
|
54
|
+
* @param {any} [debugValues]
|
|
56
55
|
* Some additional debugging values
|
|
57
56
|
* @return {YError}
|
|
58
57
|
* The wrapped error
|
|
59
58
|
*/
|
|
60
|
-
static bump<
|
|
59
|
+
static bump<T extends unknown[] = YErrorDebugValue>(err: Error | YError, errorCode?: string, debugValues?: T): YError;
|
|
61
60
|
toString(): string;
|
|
62
61
|
}
|
|
63
62
|
/**
|
|
64
|
-
* Allow to print a stack from anything (especially
|
|
63
|
+
* Allow to print a stack from anything (especially caught
|
|
65
64
|
* errors that may or may not contain errors 🤷).
|
|
66
65
|
* @param {Error} err
|
|
67
66
|
* The error to print
|
|
@@ -69,4 +68,26 @@ declare class YError extends Error {
|
|
|
69
68
|
* The stack trace if any
|
|
70
69
|
*/
|
|
71
70
|
export declare function printStackTrace(err: Error | YError): string;
|
|
71
|
+
export declare function looksLikeAYErrorCode(str: string): boolean;
|
|
72
|
+
export declare function looksLikeAYError(err: Error | YError): err is YError;
|
|
73
|
+
/**
|
|
74
|
+
* Allow to check a YError code and cast the error.
|
|
75
|
+
* @param {Error} err
|
|
76
|
+
* The error to cast
|
|
77
|
+
* @param {Error} code
|
|
78
|
+
* The code to check
|
|
79
|
+
* @return {boolean}
|
|
80
|
+
* The result
|
|
81
|
+
*/
|
|
82
|
+
export declare function hasYErrorCode<T extends unknown[] = YErrorDebugValue>(err: Error | YError, code: string): err is YError<T>;
|
|
83
|
+
/**
|
|
84
|
+
* Allow to check all errors for a YError code and return the casted the error.
|
|
85
|
+
* @param {Error} err
|
|
86
|
+
* The error to cast
|
|
87
|
+
* @param {Error} code
|
|
88
|
+
* The code to check
|
|
89
|
+
* @return {boolean}
|
|
90
|
+
* The result
|
|
91
|
+
*/
|
|
92
|
+
export declare function pickYErrorWithCode<T extends unknown[] = YErrorDebugValue>(err: Error | YError, code: string): YError<T> | null;
|
|
72
93
|
export { YError };
|
package/dist/index.js
CHANGED
|
@@ -1,25 +1,27 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { EOL } from 'node:os';
|
|
2
2
|
/**
|
|
3
|
-
* A YError class able to contain some
|
|
3
|
+
* A YError class able to contain some debugValues and
|
|
4
4
|
* print better stack traces
|
|
5
5
|
* @extends Error
|
|
6
6
|
*/
|
|
7
7
|
class YError extends Error {
|
|
8
8
|
code;
|
|
9
|
-
|
|
10
|
-
wrappedErrors;
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
9
|
+
debugValues = [];
|
|
10
|
+
wrappedErrors = [];
|
|
11
|
+
/**
|
|
12
|
+
* Creates a new YError with an error code
|
|
13
|
+
* and some debugValues as debug values.
|
|
14
|
+
* @param {string} [errorCode = 'E_UNEXPECTED']
|
|
15
|
+
* The error code corresponding to the actual error
|
|
16
|
+
* @param {any} [debugValues]
|
|
17
|
+
* Some additional debugging values
|
|
18
|
+
*/
|
|
19
|
+
constructor(errorCode, debugValues = [], wrappedErrors = []) {
|
|
18
20
|
// Call the parent constructor
|
|
19
21
|
super(errorCode);
|
|
20
22
|
// Filling error
|
|
21
23
|
this.code = errorCode || 'E_UNEXPECTED';
|
|
22
|
-
this.
|
|
24
|
+
this.debugValues = debugValues;
|
|
23
25
|
this.wrappedErrors = wrappedErrors;
|
|
24
26
|
this.name = this.toString();
|
|
25
27
|
if (Error.captureStackTrace) {
|
|
@@ -28,19 +30,19 @@ class YError extends Error {
|
|
|
28
30
|
}
|
|
29
31
|
/**
|
|
30
32
|
* Wraps any error and output a YError with an error
|
|
31
|
-
* code and some
|
|
33
|
+
* code and some debugValues as debug values.
|
|
32
34
|
* @param {Error} err
|
|
33
35
|
* The error to wrap
|
|
34
36
|
* @param {string} [errorCode = 'E_UNEXPECTED']
|
|
35
37
|
* The error code corresponding to the actual error
|
|
36
|
-
* @param {
|
|
38
|
+
* @param {any} [debugValues]
|
|
37
39
|
* Some additional debugging values
|
|
38
40
|
* @return {YError}
|
|
39
41
|
* The wrapped error
|
|
40
42
|
*/
|
|
41
|
-
static wrap(err, errorCode,
|
|
42
|
-
const wrappedErrorIsACode =
|
|
43
|
-
const wrappedErrors = ('wrappedErrors' in err ? err.wrappedErrors : []).concat(err);
|
|
43
|
+
static wrap(err, errorCode, debugValues = []) {
|
|
44
|
+
const wrappedErrorIsACode = looksLikeAYErrorCode(err.message);
|
|
45
|
+
const wrappedErrors = ('wrappedErrors' in err ? err.wrappedErrors : []).concat([err]);
|
|
44
46
|
if (!errorCode) {
|
|
45
47
|
if (wrappedErrorIsACode) {
|
|
46
48
|
errorCode = err.message;
|
|
@@ -49,28 +51,25 @@ class YError extends Error {
|
|
|
49
51
|
errorCode = 'E_UNEXPECTED';
|
|
50
52
|
}
|
|
51
53
|
}
|
|
52
|
-
|
|
53
|
-
params.push(err.message);
|
|
54
|
-
}
|
|
55
|
-
return new YError(wrappedErrors, errorCode, ...params);
|
|
54
|
+
return new YError(errorCode, debugValues, wrappedErrors);
|
|
56
55
|
}
|
|
57
56
|
/**
|
|
58
57
|
* Return a YError as is or wraps any other error and output
|
|
59
|
-
* a YError with a code and some
|
|
58
|
+
* a YError with a code and some debugValues as debug values.
|
|
60
59
|
* @param {Error} err
|
|
61
60
|
* The error to cast
|
|
62
61
|
* @param {string} [errorCode = 'E_UNEXPECTED']
|
|
63
62
|
* The error code corresponding to the actual error
|
|
64
|
-
* @param {
|
|
63
|
+
* @param {any} [debugValues]
|
|
65
64
|
* Some additional debugging values
|
|
66
65
|
* @return {YError}
|
|
67
66
|
* The wrapped error
|
|
68
67
|
*/
|
|
69
|
-
static cast(err, errorCode,
|
|
70
|
-
if (
|
|
68
|
+
static cast(err, errorCode, debugValues = []) {
|
|
69
|
+
if (looksLikeAYError(err)) {
|
|
71
70
|
return err;
|
|
72
71
|
}
|
|
73
|
-
return YError.wrap(err, errorCode,
|
|
72
|
+
return YError.wrap(err, errorCode, debugValues);
|
|
74
73
|
}
|
|
75
74
|
/**
|
|
76
75
|
* Same than `YError.wrap()` but preserves the code
|
|
@@ -80,32 +79,32 @@ class YError extends Error {
|
|
|
80
79
|
* The error to bump
|
|
81
80
|
* @param {string} [errorCode = 'E_UNEXPECTED']
|
|
82
81
|
* The error code corresponding to the actual error
|
|
83
|
-
* @param {
|
|
82
|
+
* @param {any} [debugValues]
|
|
84
83
|
* Some additional debugging values
|
|
85
84
|
* @return {YError}
|
|
86
85
|
* The wrapped error
|
|
87
86
|
*/
|
|
88
|
-
static bump(err, errorCode,
|
|
89
|
-
if (
|
|
90
|
-
return YError.wrap(err, err.code,
|
|
87
|
+
static bump(err, errorCode, debugValues = []) {
|
|
88
|
+
if (looksLikeAYError(err)) {
|
|
89
|
+
return YError.wrap(err, err.code, err.debugValues);
|
|
91
90
|
}
|
|
92
|
-
return YError.wrap(err, errorCode,
|
|
91
|
+
return YError.wrap(err, errorCode, debugValues);
|
|
93
92
|
}
|
|
94
93
|
toString() {
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
this.
|
|
104
|
-
'
|
|
94
|
+
let debugValuesAsString;
|
|
95
|
+
try {
|
|
96
|
+
debugValuesAsString = JSON.stringify(this.debugValues);
|
|
97
|
+
}
|
|
98
|
+
catch {
|
|
99
|
+
debugValuesAsString = '<circular>';
|
|
100
|
+
}
|
|
101
|
+
return `${this.wrappedErrors.length
|
|
102
|
+
? this.wrappedErrors[this.wrappedErrors.length - 1].stack + EOL
|
|
103
|
+
: ''}${this.constructor.name}: ${this.code} (${debugValuesAsString})`;
|
|
105
104
|
}
|
|
106
105
|
}
|
|
107
106
|
/**
|
|
108
|
-
* Allow to print a stack from anything (especially
|
|
107
|
+
* Allow to print a stack from anything (especially caught
|
|
109
108
|
* errors that may or may not contain errors 🤷).
|
|
110
109
|
* @param {Error} err
|
|
111
110
|
* The error to print
|
|
@@ -119,22 +118,51 @@ export function printStackTrace(err) {
|
|
|
119
118
|
? err.toString()
|
|
120
119
|
: typeof err}`;
|
|
121
120
|
}
|
|
121
|
+
export function looksLikeAYErrorCode(str) {
|
|
122
|
+
return /^([A-Z0-9_]+)$/.test(str);
|
|
123
|
+
}
|
|
122
124
|
// In order to keep compatibility through major versions
|
|
123
|
-
// we have to make kind of
|
|
124
|
-
function
|
|
125
|
+
// we have to make kind of a cross major version instanceof
|
|
126
|
+
export function looksLikeAYError(err) {
|
|
125
127
|
return (!!(err instanceof YError) ||
|
|
126
128
|
!!(err.constructor &&
|
|
127
129
|
err.constructor.name &&
|
|
128
130
|
err.constructor.name.endsWith('Error') &&
|
|
129
131
|
'code' in err &&
|
|
130
132
|
'string' === typeof err.code &&
|
|
131
|
-
|
|
132
|
-
'
|
|
133
|
-
err.
|
|
134
|
-
err.
|
|
133
|
+
looksLikeAYErrorCode(err.code) &&
|
|
134
|
+
'debugValues' in err &&
|
|
135
|
+
err.debugValues &&
|
|
136
|
+
err.debugValues instanceof Array));
|
|
135
137
|
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
+
/**
|
|
139
|
+
* Allow to check a YError code and cast the error.
|
|
140
|
+
* @param {Error} err
|
|
141
|
+
* The error to cast
|
|
142
|
+
* @param {Error} code
|
|
143
|
+
* The code to check
|
|
144
|
+
* @return {boolean}
|
|
145
|
+
* The result
|
|
146
|
+
*/
|
|
147
|
+
export function hasYErrorCode(err, code) {
|
|
148
|
+
return looksLikeAYError(err) && err.code === code;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Allow to check all errors for a YError code and return the casted the error.
|
|
152
|
+
* @param {Error} err
|
|
153
|
+
* The error to cast
|
|
154
|
+
* @param {Error} code
|
|
155
|
+
* The code to check
|
|
156
|
+
* @return {boolean}
|
|
157
|
+
* The result
|
|
158
|
+
*/
|
|
159
|
+
export function pickYErrorWithCode(err, code) {
|
|
160
|
+
for (const currentError of [err].concat('wrappedErrors' in err ? err.wrappedErrors : [])) {
|
|
161
|
+
if (hasYErrorCode(currentError, code)) {
|
|
162
|
+
return currentError;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return null;
|
|
138
166
|
}
|
|
139
167
|
export { YError };
|
|
140
168
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,SAAS,CAAC;AAI9B;;;;GAIG;AACH,MAAM,MAA+C,SAAQ,KAAK;IAChE,IAAI,CAAS;IACb,WAAW,GAAM,EAAkB,CAAC;IACpC,aAAa,GAAuB,EAAE,CAAC;IACvC;;;;;;;OAOG;IACH,YACE,SAAkB,EAClB,cAAiB,EAAkB,EACnC,gBAAoC,EAAE;QAEtC,8BAA8B;QAC9B,KAAK,CAAC,SAAS,CAAC,CAAC;QAEjB,gBAAgB;QAChB,IAAI,CAAC,IAAI,GAAG,SAAS,IAAI,cAAc,CAAC;QACxC,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC;QAC/B,IAAI,CAAC,aAAa,GAAG,aAAa,CAAC;QACnC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,QAAQ,EAAE,CAAC;QAE5B,IAAI,KAAK,CAAC,iBAAiB,EAAE,CAAC;YAC5B,KAAK,CAAC,iBAAiB,CAAC,IAAI,EAAE,IAAI,CAAC,WAAW,CAAC,CAAC;QAClD,CAAC;IACH,CAAC;IAED;;;;;;;;;;;OAWG;IACH,MAAM,CAAC,IAAI,CACT,GAAmB,EACnB,SAAkB,EAClB,cAAiB,EAAkB;QAEnC,MAAM,mBAAmB,GAAG,oBAAoB,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;QAC9D,MAAM,aAAa,GAAG,CACpB,eAAe,IAAI,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC,CAAC,EAAE,CAChD,CA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