yerror 9.1.1 → 11.0.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 +22 -0
- package/README.md +56 -28
- package/dist/index.d.ts +28 -20
- package/dist/index.js +57 -41
- package/dist/index.js.map +1 -1
- package/dist/index.test.d.ts +11 -0
- package/dist/index.test.js +109 -65
- package/dist/index.test.js.map +1 -1
- package/package.json +1 -1
- package/src/index.test.ts +155 -71
- package/src/index.ts +97 -74
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,25 @@
|
|
|
1
|
+
# [11.0.0](https://github.com/nfroidure/yerror/compare/v10.0.0...v11.0.0) (2026-04-07)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* **types:** better type inference ([283ba75](https://github.com/nfroidure/yerror/commit/283ba75bde7a4c8e7a89bb6a26866f8c43659c1b))
|
|
7
|
+
|
|
8
|
+
# [10.0.0](https://github.com/nfroidure/yerror/compare/v9.1.1...v10.0.0) (2026-04-03)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Features
|
|
12
|
+
|
|
13
|
+
* rewrite YError to use native Error.cause and Global Registry ([14ecd7d](https://github.com/nfroidure/yerror/commit/14ecd7d0956de0d1ec14f9e9a1377327af81ae46))
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
### BREAKING CHANGES
|
|
17
|
+
|
|
18
|
+
* - 'wrappedErrors' property is removed in favor of the standard 'cause' property.
|
|
19
|
+
- 'debugValues' property is renamed to 'debug' for brevity.
|
|
20
|
+
- Enhanced TypeScript support with 'YErrorRegistry' for global error typing.
|
|
21
|
+
- 'printStackTrace' is now recursive through the 'cause' chain.
|
|
22
|
+
|
|
1
23
|
## [9.1.1](https://github.com/nfroidure/yerror/compare/v9.1.0...v9.1.1) (2026-03-30)
|
|
2
24
|
|
|
3
25
|
# [9.1.0](https://github.com/nfroidure/yerror/compare/v9.0.0...v9.1.0) (2026-03-28)
|
package/README.md
CHANGED
|
@@ -12,6 +12,9 @@
|
|
|
12
12
|
|
|
13
13
|
[//]: # (::contents:start)
|
|
14
14
|
|
|
15
|
+
A robust Error subclass with error codes, typed debug values, and recursive
|
|
16
|
+
stack traces using native Error.cause.
|
|
17
|
+
|
|
15
18
|
## Usage
|
|
16
19
|
|
|
17
20
|
First, require me where you could throw errors:
|
|
@@ -24,15 +27,14 @@ Then, emit errors with a bonus: parameters!
|
|
|
24
27
|
|
|
25
28
|
```js
|
|
26
29
|
function doSomething(pay, action) {
|
|
27
|
-
if(parseInt(pay, 10) !== pay) {
|
|
28
|
-
throw new YError('E_BAD_PAY', pay, action);
|
|
30
|
+
if (parseInt(pay, 10) !== pay) {
|
|
31
|
+
throw new YError('E_BAD_PAY', [pay, action]);
|
|
29
32
|
}
|
|
30
33
|
}
|
|
31
34
|
|
|
32
35
|
doSomething('nuts', 'code');
|
|
33
36
|
|
|
34
|
-
|
|
35
|
-
// YError: E_BAD_PAY (nuts, code)
|
|
37
|
+
// YError: E_BAD_PAY (["nuts", "code"])
|
|
36
38
|
// at doSomething (/home/nfroidure/nfroidure/yerror/test.js:5:11)
|
|
37
39
|
// at Object.<anonymous> (/home/nfroidure/nfroidure/yerror/test.js:9:1)
|
|
38
40
|
// (...)
|
|
@@ -45,31 +47,56 @@ Also, you could want to wrap errors and keep a valuable stack trace:
|
|
|
45
47
|
|
|
46
48
|
```js
|
|
47
49
|
function doSomethingAsync(pay, action) {
|
|
48
|
-
return
|
|
50
|
+
return new Promise(function (resolve, reject) {
|
|
49
51
|
try {
|
|
50
52
|
doSomething(pay, action);
|
|
51
53
|
resolve();
|
|
52
|
-
} catch(err) {
|
|
54
|
+
} catch (err) {
|
|
53
55
|
reject(YError.bump(err));
|
|
54
56
|
}
|
|
55
57
|
});
|
|
56
58
|
}
|
|
57
59
|
|
|
58
|
-
doSomethingAsync('nuts', 'code')
|
|
59
|
-
.
|
|
60
|
-
|
|
61
|
-
});
|
|
60
|
+
doSomethingAsync('nuts', 'code').catch(function (err) {
|
|
61
|
+
console.log(err.stack);
|
|
62
|
+
});
|
|
62
63
|
|
|
63
64
|
// YError: E_BAD_PAY (nuts, code)
|
|
64
65
|
// at doSomething (/home/nfroidure/nfroidure/yerror/test.js:5:11)
|
|
65
66
|
// (...)
|
|
66
|
-
// YError: E_BAD_TRANSACTION (pay)
|
|
67
|
+
// Caused by: YError: E_BAD_TRANSACTION (pay)
|
|
67
68
|
// at Function.YError.wrap (/home/nfroidure/nfroidure/yerror/src/index.js:41:12)
|
|
68
69
|
// at /home/nfroidure/nfroidure/yerror/test.js:16:21
|
|
69
70
|
// at doSomethingAsync (/home/nfroidure/nfroidure/yerror/test.js:11:11)
|
|
70
71
|
// (...)
|
|
71
72
|
```
|
|
72
73
|
|
|
74
|
+
## Global Error Registry
|
|
75
|
+
|
|
76
|
+
You can now get full autocompletion and type-safety for your error codes and
|
|
77
|
+
their debug data.
|
|
78
|
+
|
|
79
|
+
```ts
|
|
80
|
+
import { YError } from 'yerror';
|
|
81
|
+
|
|
82
|
+
declare module 'yerror' {
|
|
83
|
+
interface YErrorRegistry {
|
|
84
|
+
E_USER_NOT_FOUND: [userId: string];
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// TypeScript will now enforce the correct arguments:
|
|
89
|
+
throw new YError('E_USER_NOT_FOUND', ['123']);
|
|
90
|
+
|
|
91
|
+
// Users of you own code will then be able to cast errors
|
|
92
|
+
try {
|
|
93
|
+
getUser('123');
|
|
94
|
+
} catch (err) {
|
|
95
|
+
if (hasYErrorCode(err, 'E_USER_NOT_FOUND')) {
|
|
96
|
+
console.log(err.debug[0]);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
```
|
|
73
100
|
|
|
74
101
|
[//]: # (::contents:end)
|
|
75
102
|
|
|
@@ -78,7 +105,7 @@ doSomethingAsync('nuts', 'code')
|
|
|
78
105
|
|
|
79
106
|
<dl>
|
|
80
107
|
<dt><a href="#YError">YError</a> ⇐ <code>Error</code></dt>
|
|
81
|
-
<dd><p>A YError class able to contain some
|
|
108
|
+
<dd><p>A YError class able to contain some debug and
|
|
82
109
|
print better stack traces</p>
|
|
83
110
|
</dd>
|
|
84
111
|
</dl>
|
|
@@ -101,35 +128,36 @@ doSomethingAsync('nuts', 'code')
|
|
|
101
128
|
<a name="YError"></a>
|
|
102
129
|
|
|
103
130
|
## YError ⇐ <code>Error</code>
|
|
104
|
-
A YError class able to contain some
|
|
131
|
+
A YError class able to contain some debug and
|
|
105
132
|
print better stack traces
|
|
106
133
|
|
|
107
134
|
**Kind**: global class
|
|
108
135
|
**Extends**: <code>Error</code>
|
|
109
136
|
|
|
110
137
|
* [YError](#YError) ⇐ <code>Error</code>
|
|
111
|
-
* [new YError([errorCode], [
|
|
112
|
-
* [.wrap(err, [errorCode], [
|
|
113
|
-
* [.cast(err, [errorCode], [
|
|
114
|
-
* [.bump(err, [errorCode], [
|
|
138
|
+
* [new YError([errorCode], [debug], options)](#new_YError_new)
|
|
139
|
+
* [.wrap(err, [errorCode], [debug])](#YError.wrap) ⇒ [<code>YError</code>](#YError)
|
|
140
|
+
* [.cast(err, [errorCode], [debug])](#YError.cast) ⇒ [<code>YError</code>](#YError)
|
|
141
|
+
* [.bump(err, [errorCode], [debug])](#YError.bump) ⇒ [<code>YError</code>](#YError)
|
|
115
142
|
|
|
116
143
|
<a name="new_YError_new"></a>
|
|
117
144
|
|
|
118
|
-
### new YError([errorCode], [
|
|
145
|
+
### new YError([errorCode], [debug], options)
|
|
119
146
|
Creates a new YError with an error code
|
|
120
|
-
and some
|
|
147
|
+
and some debug as debug values.
|
|
121
148
|
|
|
122
149
|
|
|
123
150
|
| Param | Type | Default | Description |
|
|
124
151
|
| --- | --- | --- | --- |
|
|
125
152
|
| [errorCode] | <code>string</code> | <code>"'E_UNEXPECTED'"</code> | The error code corresponding to the actual error |
|
|
126
|
-
| [
|
|
153
|
+
| [debug] | <code>any</code> | | Some additional debugging values The error options |
|
|
154
|
+
| options | <code>Object</code> | | The error options |
|
|
127
155
|
|
|
128
156
|
<a name="YError.wrap"></a>
|
|
129
157
|
|
|
130
|
-
### YError.wrap(err, [errorCode], [
|
|
158
|
+
### YError.wrap(err, [errorCode], [debug]) ⇒ [<code>YError</code>](#YError)
|
|
131
159
|
Wraps any error and output a YError with an error
|
|
132
|
-
code and some
|
|
160
|
+
code and some debug as debug values.
|
|
133
161
|
|
|
134
162
|
**Kind**: static method of [<code>YError</code>](#YError)
|
|
135
163
|
**Returns**: [<code>YError</code>](#YError) - The wrapped error
|
|
@@ -138,13 +166,13 @@ Wraps any error and output a YError with an error
|
|
|
138
166
|
| --- | --- | --- | --- |
|
|
139
167
|
| err | <code>Error</code> | | The error to wrap |
|
|
140
168
|
| [errorCode] | <code>string</code> | <code>"'E_UNEXPECTED'"</code> | The error code corresponding to the actual error |
|
|
141
|
-
| [
|
|
169
|
+
| [debug] | <code>any</code> | | Some additional debugging values |
|
|
142
170
|
|
|
143
171
|
<a name="YError.cast"></a>
|
|
144
172
|
|
|
145
|
-
### YError.cast(err, [errorCode], [
|
|
173
|
+
### YError.cast(err, [errorCode], [debug]) ⇒ [<code>YError</code>](#YError)
|
|
146
174
|
Return a YError as is or wraps any other error and output
|
|
147
|
-
a YError with a code and some
|
|
175
|
+
a YError with a code and some debug as debug values.
|
|
148
176
|
|
|
149
177
|
**Kind**: static method of [<code>YError</code>](#YError)
|
|
150
178
|
**Returns**: [<code>YError</code>](#YError) - The wrapped error
|
|
@@ -153,11 +181,11 @@ Return a YError as is or wraps any other error and output
|
|
|
153
181
|
| --- | --- | --- | --- |
|
|
154
182
|
| err | <code>Error</code> | | The error to cast |
|
|
155
183
|
| [errorCode] | <code>string</code> | <code>"'E_UNEXPECTED'"</code> | The error code corresponding to the actual error |
|
|
156
|
-
| [
|
|
184
|
+
| [debug] | <code>any</code> | | Some additional debugging values |
|
|
157
185
|
|
|
158
186
|
<a name="YError.bump"></a>
|
|
159
187
|
|
|
160
|
-
### YError.bump(err, [errorCode], [
|
|
188
|
+
### YError.bump(err, [errorCode], [debug]) ⇒ [<code>YError</code>](#YError)
|
|
161
189
|
Same than `YError.wrap()` but preserves the code
|
|
162
190
|
and the debug values of the error if it is
|
|
163
191
|
already an instance of the YError constructor.
|
|
@@ -169,7 +197,7 @@ Same than `YError.wrap()` but preserves the code
|
|
|
169
197
|
| --- | --- | --- | --- |
|
|
170
198
|
| err | <code>Error</code> | | The error to bump |
|
|
171
199
|
| [errorCode] | <code>string</code> | <code>"'E_UNEXPECTED'"</code> | The error code corresponding to the actual error |
|
|
172
|
-
| [
|
|
200
|
+
| [debug] | <code>any</code> | | Some additional debugging values |
|
|
173
201
|
|
|
174
202
|
<a name="printStackTrace"></a>
|
|
175
203
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,48 +1,56 @@
|
|
|
1
|
-
export
|
|
1
|
+
export interface YErrorRegistry {
|
|
2
|
+
E_UNEXPECTED: unknown[];
|
|
3
|
+
}
|
|
4
|
+
export type YErrorDebug<C extends string> = C extends keyof YErrorRegistry ? YErrorRegistry[C] extends unknown[] ? YErrorRegistry[C] : never : unknown[];
|
|
2
5
|
/**
|
|
3
|
-
* A YError class able to contain some
|
|
6
|
+
* A YError class able to contain some debug and
|
|
4
7
|
* print better stack traces
|
|
5
8
|
* @extends Error
|
|
6
9
|
*/
|
|
7
|
-
declare class YError<
|
|
10
|
+
declare class YError<C extends string = string, CC extends string = string> extends Error {
|
|
8
11
|
code: string;
|
|
9
|
-
|
|
10
|
-
|
|
12
|
+
debug: YErrorDebug<C>;
|
|
13
|
+
cause?: Error | YError<CC> | undefined;
|
|
11
14
|
/**
|
|
12
15
|
* Creates a new YError with an error code
|
|
13
|
-
* and some
|
|
16
|
+
* and some debug as debug values.
|
|
14
17
|
* @param {string} [errorCode = 'E_UNEXPECTED']
|
|
15
18
|
* The error code corresponding to the actual error
|
|
16
|
-
* @param {any} [
|
|
19
|
+
* @param {any} [debug]
|
|
17
20
|
* Some additional debugging values
|
|
21
|
+
* The error options
|
|
22
|
+
* @param {Object} options
|
|
23
|
+
* The error options
|
|
18
24
|
*/
|
|
19
|
-
constructor(errorCode?:
|
|
25
|
+
constructor(errorCode?: C, debug?: YErrorDebug<C>, options?: {
|
|
26
|
+
cause?: Error | YError<CC>;
|
|
27
|
+
});
|
|
20
28
|
/**
|
|
21
29
|
* Wraps any error and output a YError with an error
|
|
22
|
-
* code and some
|
|
30
|
+
* code and some debug as debug values.
|
|
23
31
|
* @param {Error} err
|
|
24
32
|
* The error to wrap
|
|
25
33
|
* @param {string} [errorCode = 'E_UNEXPECTED']
|
|
26
34
|
* The error code corresponding to the actual error
|
|
27
|
-
* @param {any} [
|
|
35
|
+
* @param {any} [debug]
|
|
28
36
|
* Some additional debugging values
|
|
29
37
|
* @return {YError}
|
|
30
38
|
* The wrapped error
|
|
31
39
|
*/
|
|
32
|
-
static wrap<
|
|
40
|
+
static wrap<C extends string, CC extends string>(err: Error | YError<CC>, errorCode?: C, debug?: YErrorDebug<C>): YError<C, CC>;
|
|
33
41
|
/**
|
|
34
42
|
* Return a YError as is or wraps any other error and output
|
|
35
|
-
* a YError with a code and some
|
|
43
|
+
* a YError with a code and some debug as debug values.
|
|
36
44
|
* @param {Error} err
|
|
37
45
|
* The error to cast
|
|
38
46
|
* @param {string} [errorCode = 'E_UNEXPECTED']
|
|
39
47
|
* The error code corresponding to the actual error
|
|
40
|
-
* @param {any} [
|
|
48
|
+
* @param {any} [debug]
|
|
41
49
|
* Some additional debugging values
|
|
42
50
|
* @return {YError}
|
|
43
51
|
* The wrapped error
|
|
44
52
|
*/
|
|
45
|
-
static cast<
|
|
53
|
+
static cast<C extends string, CC extends string>(err: Error | YError<C> | YError<CC>, errorCode?: C, debug?: YErrorDebug<C>): YError<C> | YError<C, CC>;
|
|
46
54
|
/**
|
|
47
55
|
* Same than `YError.wrap()` but preserves the code
|
|
48
56
|
* and the debug values of the error if it is
|
|
@@ -51,12 +59,12 @@ declare class YError<T extends unknown[] = YErrorDebugValue> extends Error {
|
|
|
51
59
|
* The error to bump
|
|
52
60
|
* @param {string} [errorCode = 'E_UNEXPECTED']
|
|
53
61
|
* The error code corresponding to the actual error
|
|
54
|
-
* @param {any} [
|
|
62
|
+
* @param {any} [debug]
|
|
55
63
|
* Some additional debugging values
|
|
56
64
|
* @return {YError}
|
|
57
65
|
* The wrapped error
|
|
58
66
|
*/
|
|
59
|
-
static bump<
|
|
67
|
+
static bump<C extends string, CC extends string>(err: Error | YError<CC>, errorCode?: C, debug?: YErrorDebug<C>): YError<C, CC> | YError<CC, CC>;
|
|
60
68
|
toString(): string;
|
|
61
69
|
}
|
|
62
70
|
/**
|
|
@@ -67,9 +75,9 @@ declare class YError<T extends unknown[] = YErrorDebugValue> extends Error {
|
|
|
67
75
|
* @return {string}
|
|
68
76
|
* The stack trace if any
|
|
69
77
|
*/
|
|
70
|
-
export declare function printStackTrace(err: Error | YError): string;
|
|
78
|
+
export declare function printStackTrace(err: Error | YError | unknown): string;
|
|
71
79
|
export declare function looksLikeAYErrorCode(str: string): boolean;
|
|
72
|
-
export declare function looksLikeAYError(err: Error | YError): err is YError;
|
|
80
|
+
export declare function looksLikeAYError(err: Error | YError | unknown): err is YError;
|
|
73
81
|
/**
|
|
74
82
|
* Allow to check a YError code and cast the error.
|
|
75
83
|
* @param {Error} err
|
|
@@ -79,7 +87,7 @@ export declare function looksLikeAYError(err: Error | YError): err is YError;
|
|
|
79
87
|
* @return {boolean}
|
|
80
88
|
* The result
|
|
81
89
|
*/
|
|
82
|
-
export declare function hasYErrorCode<
|
|
90
|
+
export declare function hasYErrorCode<C extends string>(err: Error | YError | unknown, code: C): err is YError<C>;
|
|
83
91
|
/**
|
|
84
92
|
* Allow to check all errors for a YError code and return the casted the error.
|
|
85
93
|
* @param {Error} err
|
|
@@ -89,5 +97,5 @@ export declare function hasYErrorCode<T extends unknown[] = YErrorDebugValue>(er
|
|
|
89
97
|
* @return {boolean}
|
|
90
98
|
* The result
|
|
91
99
|
*/
|
|
92
|
-
export declare function pickYErrorWithCode<
|
|
100
|
+
export declare function pickYErrorWithCode<C extends string>(err: Error | YError | unknown, code: C): YError<C> | null;
|
|
93
101
|
export { YError };
|
package/dist/index.js
CHANGED
|
@@ -1,28 +1,31 @@
|
|
|
1
1
|
import { EOL } from 'node:os';
|
|
2
2
|
/**
|
|
3
|
-
* A YError class able to contain some
|
|
3
|
+
* A YError class able to contain some debug and
|
|
4
4
|
* print better stack traces
|
|
5
5
|
* @extends Error
|
|
6
6
|
*/
|
|
7
7
|
class YError extends Error {
|
|
8
8
|
code;
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
debug;
|
|
10
|
+
cause;
|
|
11
11
|
/**
|
|
12
12
|
* Creates a new YError with an error code
|
|
13
|
-
* and some
|
|
13
|
+
* and some debug as debug values.
|
|
14
14
|
* @param {string} [errorCode = 'E_UNEXPECTED']
|
|
15
15
|
* The error code corresponding to the actual error
|
|
16
|
-
* @param {any} [
|
|
16
|
+
* @param {any} [debug]
|
|
17
17
|
* Some additional debugging values
|
|
18
|
+
* The error options
|
|
19
|
+
* @param {Object} options
|
|
20
|
+
* The error options
|
|
18
21
|
*/
|
|
19
|
-
constructor(errorCode
|
|
22
|
+
constructor(errorCode = 'E_UNEXPECTED', debug, options = {}) {
|
|
20
23
|
// Call the parent constructor
|
|
21
24
|
super(errorCode);
|
|
22
25
|
// Filling error
|
|
23
|
-
this.code = errorCode
|
|
24
|
-
this.
|
|
25
|
-
this.
|
|
26
|
+
this.code = errorCode;
|
|
27
|
+
this.debug = (debug || []);
|
|
28
|
+
this.cause = options.cause;
|
|
26
29
|
this.name = this.toString();
|
|
27
30
|
if (Error.captureStackTrace) {
|
|
28
31
|
Error.captureStackTrace(this, this.constructor);
|
|
@@ -30,19 +33,18 @@ class YError extends Error {
|
|
|
30
33
|
}
|
|
31
34
|
/**
|
|
32
35
|
* Wraps any error and output a YError with an error
|
|
33
|
-
* code and some
|
|
36
|
+
* code and some debug as debug values.
|
|
34
37
|
* @param {Error} err
|
|
35
38
|
* The error to wrap
|
|
36
39
|
* @param {string} [errorCode = 'E_UNEXPECTED']
|
|
37
40
|
* The error code corresponding to the actual error
|
|
38
|
-
* @param {any} [
|
|
41
|
+
* @param {any} [debug]
|
|
39
42
|
* Some additional debugging values
|
|
40
43
|
* @return {YError}
|
|
41
44
|
* The wrapped error
|
|
42
45
|
*/
|
|
43
|
-
static wrap(err, errorCode,
|
|
46
|
+
static wrap(err, errorCode, debug) {
|
|
44
47
|
const wrappedErrorIsACode = looksLikeAYErrorCode(err.message);
|
|
45
|
-
const wrappedErrors = ('wrappedErrors' in err ? err.wrappedErrors : []).concat([err]);
|
|
46
48
|
if (!errorCode) {
|
|
47
49
|
if (wrappedErrorIsACode) {
|
|
48
50
|
errorCode = err.message;
|
|
@@ -51,25 +53,25 @@ class YError extends Error {
|
|
|
51
53
|
errorCode = 'E_UNEXPECTED';
|
|
52
54
|
}
|
|
53
55
|
}
|
|
54
|
-
return new YError(errorCode,
|
|
56
|
+
return new YError(errorCode, debug, { cause: err });
|
|
55
57
|
}
|
|
56
58
|
/**
|
|
57
59
|
* Return a YError as is or wraps any other error and output
|
|
58
|
-
* a YError with a code and some
|
|
60
|
+
* a YError with a code and some debug as debug values.
|
|
59
61
|
* @param {Error} err
|
|
60
62
|
* The error to cast
|
|
61
63
|
* @param {string} [errorCode = 'E_UNEXPECTED']
|
|
62
64
|
* The error code corresponding to the actual error
|
|
63
|
-
* @param {any} [
|
|
65
|
+
* @param {any} [debug]
|
|
64
66
|
* Some additional debugging values
|
|
65
67
|
* @return {YError}
|
|
66
68
|
* The wrapped error
|
|
67
69
|
*/
|
|
68
|
-
static cast(err, errorCode,
|
|
70
|
+
static cast(err, errorCode, debug) {
|
|
69
71
|
if (looksLikeAYError(err)) {
|
|
70
72
|
return err;
|
|
71
73
|
}
|
|
72
|
-
return YError.wrap(err, errorCode,
|
|
74
|
+
return YError.wrap(err, errorCode, debug);
|
|
73
75
|
}
|
|
74
76
|
/**
|
|
75
77
|
* Same than `YError.wrap()` but preserves the code
|
|
@@ -79,28 +81,26 @@ class YError extends Error {
|
|
|
79
81
|
* The error to bump
|
|
80
82
|
* @param {string} [errorCode = 'E_UNEXPECTED']
|
|
81
83
|
* The error code corresponding to the actual error
|
|
82
|
-
* @param {any} [
|
|
84
|
+
* @param {any} [debug]
|
|
83
85
|
* Some additional debugging values
|
|
84
86
|
* @return {YError}
|
|
85
87
|
* The wrapped error
|
|
86
88
|
*/
|
|
87
|
-
static bump(err, errorCode,
|
|
89
|
+
static bump(err, errorCode, debug) {
|
|
88
90
|
if (looksLikeAYError(err)) {
|
|
89
|
-
return YError.wrap(err, err.code, err.
|
|
91
|
+
return YError.wrap(err, err.code, err.debug);
|
|
90
92
|
}
|
|
91
|
-
return YError.wrap(err, errorCode,
|
|
93
|
+
return YError.wrap(err, errorCode, debug);
|
|
92
94
|
}
|
|
93
95
|
toString() {
|
|
94
|
-
let
|
|
96
|
+
let debugAsString;
|
|
95
97
|
try {
|
|
96
|
-
|
|
98
|
+
debugAsString = JSON.stringify(this.debug);
|
|
97
99
|
}
|
|
98
100
|
catch {
|
|
99
|
-
|
|
101
|
+
debugAsString = '<circular>';
|
|
100
102
|
}
|
|
101
|
-
return `${this.
|
|
102
|
-
? this.wrappedErrors[this.wrappedErrors.length - 1].stack + EOL
|
|
103
|
-
: ''}${this.constructor.name}: ${this.code} (${debugValuesAsString})`;
|
|
103
|
+
return `${this.constructor.name}: ${this.code} (${debugAsString})`;
|
|
104
104
|
}
|
|
105
105
|
}
|
|
106
106
|
/**
|
|
@@ -112,11 +112,23 @@ class YError extends Error {
|
|
|
112
112
|
* The stack trace if any
|
|
113
113
|
*/
|
|
114
114
|
export function printStackTrace(err) {
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
115
|
+
let errorAsString;
|
|
116
|
+
if (typeof err === 'object' &&
|
|
117
|
+
err instanceof Error &&
|
|
118
|
+
typeof err.stack === 'string') {
|
|
119
|
+
errorAsString = err.stack;
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
try {
|
|
123
|
+
errorAsString = `[no_stack_trace]: error is serializable (${JSON.stringify(err)})`;
|
|
124
|
+
}
|
|
125
|
+
catch {
|
|
126
|
+
errorAsString = `[no_stack_trace]: error is circular ("${err != null && typeof err.toString === 'function'
|
|
127
|
+
? err.toString()
|
|
128
|
+
: typeof err}")`;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return `${errorAsString}${err instanceof Error && err.cause ? EOL + 'Caused by: ' + printStackTrace(err.cause) : ''}`;
|
|
120
132
|
}
|
|
121
133
|
export function looksLikeAYErrorCode(str) {
|
|
122
134
|
return /^([A-Z0-9_]+)$/.test(str);
|
|
@@ -125,15 +137,17 @@ export function looksLikeAYErrorCode(str) {
|
|
|
125
137
|
// we have to make kind of a cross major version instanceof
|
|
126
138
|
export function looksLikeAYError(err) {
|
|
127
139
|
return (!!(err instanceof YError) ||
|
|
128
|
-
!!(err
|
|
140
|
+
!!(err &&
|
|
141
|
+
typeof err === 'object' &&
|
|
142
|
+
err.constructor &&
|
|
129
143
|
err.constructor.name &&
|
|
130
144
|
err.constructor.name.endsWith('Error') &&
|
|
131
145
|
'code' in err &&
|
|
132
146
|
'string' === typeof err.code &&
|
|
133
147
|
looksLikeAYErrorCode(err.code) &&
|
|
134
|
-
'
|
|
135
|
-
err.
|
|
136
|
-
err.
|
|
148
|
+
'debug' in err &&
|
|
149
|
+
err.debug &&
|
|
150
|
+
err.debug instanceof Array));
|
|
137
151
|
}
|
|
138
152
|
/**
|
|
139
153
|
* Allow to check a YError code and cast the error.
|
|
@@ -157,11 +171,13 @@ export function hasYErrorCode(err, code) {
|
|
|
157
171
|
* The result
|
|
158
172
|
*/
|
|
159
173
|
export function pickYErrorWithCode(err, code) {
|
|
160
|
-
|
|
161
|
-
if (hasYErrorCode(
|
|
162
|
-
return
|
|
174
|
+
do {
|
|
175
|
+
if (hasYErrorCode(err, code)) {
|
|
176
|
+
return err;
|
|
163
177
|
}
|
|
164
|
-
|
|
178
|
+
err =
|
|
179
|
+
err && typeof err === 'object' && 'cause' in err && err.cause;
|
|
180
|
+
} while (err);
|
|
165
181
|
return null;
|
|
166
182
|
}
|
|
167
183
|
export { YError };
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,SAAS,CAAC;AAY9B;;;;GAIG;AACH,MAAM,MAGJ,SAAQ,KAAK;IACb,IAAI,CAAS;IACb,KAAK,CAAiB;IACtB,KAAK,CAAkC;IACvC;;;;;;;;;;OAUG;IACH,YACE,YAAe,cAAmB,EAClC,KAAsB,EACtB,UAA0C,EAAE;QAE5C,8BAA8B;QAC9B,KAAK,CAAC,SAAS,CAAC,CAAC;QAEjB,gBAAgB;QAChB,IAAI,CAAC,IAAI,GAAG,SAAS,CAAC;QACtB,IAAI,CAAC,KAAK,GAAG,CAAC,KAAK,IAAI,EAAE,CAAmB,CAAC;QAC7C,IAAI,CAAC,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC;QAC3B,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,GAAuB,EACvB,SAAa,EACb,KAAsB;QAEtB,MAAM,mBAAmB,GAAG,oBAAoB,CAAC,GAAG,CAAC,OAAO,CAAC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