yerror 7.0.0 → 9.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 +6 -0
- package/README.md +28 -16
- package/dist/index.d.ts +18 -17
- package/dist/index.js +47 -48
- package/dist/index.js.map +1 -1
- package/dist/index.test.js +83 -83
- package/dist/index.test.js.map +1 -1
- package/package.json +45 -89
- package/src/index.test.ts +111 -142
- package/src/index.ts +56 -69
package/src/index.test.ts
CHANGED
|
@@ -1,137 +1,120 @@
|
|
|
1
|
-
|
|
2
|
-
import { describe, test } from '@jest/globals';
|
|
3
|
-
import assert from 'assert';
|
|
1
|
+
import { describe, test, expect } from '@jest/globals';
|
|
4
2
|
import { YError } from './index.js';
|
|
5
3
|
|
|
6
4
|
describe('YError', () => {
|
|
7
5
|
describe('.__constructor', () => {
|
|
8
6
|
test('Should work', () => {
|
|
9
|
-
const err = new YError('E_ERROR', 'arg1', 'arg2');
|
|
7
|
+
const err = new YError('E_ERROR', ['arg1', 'arg2']);
|
|
10
8
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
9
|
+
expect(err instanceof Error).toBeTruthy();
|
|
10
|
+
expect(err.name).toEqual(err.toString());
|
|
11
|
+
expect(err.code).toEqual('E_ERROR');
|
|
12
|
+
expect(err.debugValues).toEqual(['arg1', 'arg2']);
|
|
13
|
+
expect(err.toString()).toEqual('YError: E_ERROR (["arg1","arg2"])');
|
|
16
14
|
});
|
|
17
15
|
test('Should work without code', () => {
|
|
18
16
|
const err = new YError();
|
|
19
17
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
18
|
+
expect(err.code).toEqual('E_UNEXPECTED');
|
|
19
|
+
expect(err.debugValues).toEqual([]);
|
|
20
|
+
expect(err.toString()).toEqual('YError: E_UNEXPECTED ([])');
|
|
21
|
+
expect(err.name).toEqual(err.toString());
|
|
24
22
|
});
|
|
25
23
|
test('Should work without new', () => {
|
|
26
|
-
const err = new YError('E_ERROR', 'arg1', 'arg2');
|
|
24
|
+
const err = new YError('E_ERROR', ['arg1', 'arg2']);
|
|
27
25
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
26
|
+
expect(err.code).toEqual('E_ERROR');
|
|
27
|
+
expect(err instanceof YError).toBeTruthy();
|
|
28
|
+
expect(err.debugValues).toEqual(['arg1', 'arg2']);
|
|
29
|
+
expect(err.toString()).toEqual('YError: E_ERROR (["arg1","arg2"])');
|
|
30
|
+
expect(err.name).toEqual(err.toString());
|
|
33
31
|
});
|
|
34
32
|
});
|
|
35
33
|
|
|
36
34
|
describe('.wrap()', () => {
|
|
37
35
|
test('Should work with standard errors and a message', () => {
|
|
38
|
-
// eslint-disable-line
|
|
39
36
|
const err = YError.wrap(new Error('This is an error!'));
|
|
40
37
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
38
|
+
expect(err.code).toEqual('E_UNEXPECTED');
|
|
39
|
+
expect(err.wrappedErrors.length).toEqual(1);
|
|
40
|
+
expect(err.debugValues).toEqual([]);
|
|
44
41
|
|
|
45
42
|
if ('captureStackTrace' in Error) {
|
|
46
|
-
|
|
43
|
+
expect(
|
|
47
44
|
-1 !== (err.stack || '').indexOf('Error: This is an error!'),
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
'YError: E_UNEXPECTED (This is an error!)',
|
|
54
|
-
),
|
|
55
|
-
'Stack contains cast error.',
|
|
56
|
-
);
|
|
57
|
-
assert.equal(err.name, err.toString());
|
|
45
|
+
).toBeTruthy();
|
|
46
|
+
expect(
|
|
47
|
+
-1 !== (err.stack || '').indexOf('YError: E_UNEXPECTED ([])'),
|
|
48
|
+
).toBeTruthy();
|
|
49
|
+
expect(err.name).toEqual(err.toString());
|
|
58
50
|
}
|
|
59
51
|
});
|
|
60
52
|
|
|
61
53
|
test('Should work with standard errors and an error code', () => {
|
|
62
54
|
const err = YError.wrap(new Error('E_ERROR'));
|
|
63
55
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
56
|
+
expect(err.code).toEqual('E_ERROR');
|
|
57
|
+
expect(err.wrappedErrors.length).toEqual(1);
|
|
58
|
+
expect(err.debugValues).toEqual([]);
|
|
67
59
|
|
|
68
60
|
if ('captureStackTrace' in Error) {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
);
|
|
73
|
-
assert(
|
|
74
|
-
-1 !== (err.stack || '').indexOf('YError: E_ERROR ()'),
|
|
75
|
-
'Stack contains cast error.',
|
|
76
|
-
);
|
|
61
|
+
expect(-1 !== (err.stack || '').indexOf('Error: E_ERROR')).toBeTruthy();
|
|
62
|
+
expect(
|
|
63
|
+
-1 !== (err.stack || '').indexOf('YError: E_ERROR ([])'),
|
|
64
|
+
).toBeTruthy();
|
|
77
65
|
}
|
|
78
|
-
|
|
66
|
+
expect(err.name).toEqual(err.toString());
|
|
79
67
|
});
|
|
80
68
|
|
|
81
69
|
test('Should work with standard errors, an error code and params', () => {
|
|
82
|
-
const err = YError.wrap(
|
|
83
|
-
new Error('E_ERROR'),
|
|
84
|
-
'E_ERROR_2',
|
|
70
|
+
const err = YError.wrap(new Error('E_ERROR'), 'E_ERROR_2', [
|
|
85
71
|
'arg1',
|
|
86
72
|
'arg2',
|
|
87
|
-
);
|
|
73
|
+
]);
|
|
88
74
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
75
|
+
expect(err.code).toEqual('E_ERROR_2');
|
|
76
|
+
expect(err.wrappedErrors.length).toEqual(1);
|
|
77
|
+
expect(err.debugValues).toEqual(['arg1', 'arg2']);
|
|
92
78
|
|
|
93
79
|
if ('captureStackTrace' in Error) {
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
-1 !== (err.stack || '').indexOf('YError: E_ERROR_2 (arg1, arg2)'),
|
|
100
|
-
'Stack contains second error.',
|
|
101
|
-
);
|
|
80
|
+
expect(-1 !== (err.stack || '').indexOf('Error: E_ERROR')).toBeTruthy();
|
|
81
|
+
expect(
|
|
82
|
+
-1 !==
|
|
83
|
+
(err.stack || '').indexOf('YError: E_ERROR_2 (["arg1","arg2"])'),
|
|
84
|
+
).toBeTruthy();
|
|
102
85
|
}
|
|
103
|
-
|
|
86
|
+
expect(err.name).toEqual(err.toString());
|
|
104
87
|
});
|
|
105
88
|
|
|
106
89
|
test('Should work with several wrapped errors', () => {
|
|
107
90
|
const err = YError.wrap(
|
|
108
|
-
YError.wrap(new Error('E_ERROR_1'), 'E_ERROR_2', 'arg2.1', 'arg2.2'),
|
|
91
|
+
YError.wrap(new Error('E_ERROR_1'), 'E_ERROR_2', ['arg2.1', 'arg2.2']),
|
|
109
92
|
'E_ERROR_3',
|
|
110
|
-
'arg3.1',
|
|
111
|
-
'arg3.2',
|
|
93
|
+
['arg3.1', 'arg3.2'],
|
|
112
94
|
);
|
|
113
95
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
96
|
+
expect(err.code).toEqual('E_ERROR_3');
|
|
97
|
+
expect(err.wrappedErrors.length).toEqual(2);
|
|
98
|
+
expect(err.debugValues).toEqual(['arg3.1', 'arg3.2']);
|
|
117
99
|
|
|
118
100
|
if ('captureStackTrace' in Error) {
|
|
119
|
-
|
|
101
|
+
expect(
|
|
120
102
|
-1 !== (err.stack || '').indexOf('Error: E_ERROR_1'),
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
assert(
|
|
103
|
+
).toBeTruthy();
|
|
104
|
+
expect(
|
|
124
105
|
-1 !==
|
|
125
|
-
(err.stack || '').indexOf(
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
106
|
+
(err.stack || '').indexOf(
|
|
107
|
+
'YError: E_ERROR_2 (["arg2.1","arg2.2"])',
|
|
108
|
+
),
|
|
109
|
+
).toBeTruthy();
|
|
110
|
+
expect(
|
|
129
111
|
-1 !==
|
|
130
|
-
(err.stack || '').indexOf(
|
|
131
|
-
|
|
132
|
-
|
|
112
|
+
(err.stack || '').indexOf(
|
|
113
|
+
'YError: E_ERROR_3 (["arg3.1","arg3.2"])',
|
|
114
|
+
),
|
|
115
|
+
).toBeTruthy();
|
|
133
116
|
}
|
|
134
|
-
|
|
117
|
+
expect(err.name).toEqual(err.toString());
|
|
135
118
|
});
|
|
136
119
|
});
|
|
137
120
|
|
|
@@ -139,39 +122,33 @@ describe('YError', () => {
|
|
|
139
122
|
test('Should work with standard errors and a message', () => {
|
|
140
123
|
const err = YError.cast(new Error('This is an error!'));
|
|
141
124
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
125
|
+
expect(err.code).toEqual('E_UNEXPECTED');
|
|
126
|
+
expect(err.wrappedErrors.length).toEqual(1);
|
|
127
|
+
expect(err.debugValues).toEqual([]);
|
|
145
128
|
|
|
146
129
|
if ('captureStackTrace' in Error) {
|
|
147
|
-
|
|
130
|
+
expect(
|
|
148
131
|
-1 !== (err.stack || '').indexOf('Error: This is an error!'),
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
(err.stack || '').indexOf(
|
|
154
|
-
'YError: E_UNEXPECTED (This is an error!)',
|
|
155
|
-
),
|
|
156
|
-
'Stack contains cast error.',
|
|
157
|
-
);
|
|
132
|
+
).toBeTruthy();
|
|
133
|
+
expect(
|
|
134
|
+
-1 !== (err.stack || '').indexOf('YError: E_UNEXPECTED ([])'),
|
|
135
|
+
).toBeTruthy();
|
|
158
136
|
}
|
|
159
|
-
|
|
137
|
+
expect(err.name).toEqual(err.toString());
|
|
160
138
|
});
|
|
161
139
|
|
|
162
140
|
test('Should let YError instances pass through', () => {
|
|
163
|
-
const err = YError.cast(new YError('E_ERROR', 'arg1', 'arg2'));
|
|
141
|
+
const err = YError.cast(new YError('E_ERROR', ['arg1', 'arg2']));
|
|
164
142
|
|
|
165
|
-
|
|
166
|
-
|
|
143
|
+
expect(err.code).toEqual('E_ERROR');
|
|
144
|
+
expect(err.debugValues).toEqual(['arg1', 'arg2']);
|
|
167
145
|
|
|
168
146
|
if ('captureStackTrace' in Error) {
|
|
169
|
-
|
|
170
|
-
-1 !== (err.stack || '').indexOf('YError: E_ERROR (arg1,
|
|
171
|
-
|
|
172
|
-
);
|
|
147
|
+
expect(
|
|
148
|
+
-1 !== (err.stack || '').indexOf('YError: E_ERROR (["arg1","arg2"])'),
|
|
149
|
+
).toBeTruthy();
|
|
173
150
|
}
|
|
174
|
-
|
|
151
|
+
expect(err.name).toEqual(err.toString());
|
|
175
152
|
});
|
|
176
153
|
});
|
|
177
154
|
|
|
@@ -179,76 +156,68 @@ describe('YError', () => {
|
|
|
179
156
|
test('Should work with standard errors and a message', () => {
|
|
180
157
|
const err = YError.bump(new Error('This is an error!'));
|
|
181
158
|
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
159
|
+
expect(err.code).toEqual('E_UNEXPECTED');
|
|
160
|
+
expect(err.wrappedErrors.length).toEqual(1);
|
|
161
|
+
expect(err.debugValues).toEqual([]);
|
|
185
162
|
|
|
186
163
|
if ('captureStackTrace' in Error) {
|
|
187
|
-
|
|
164
|
+
expect(
|
|
188
165
|
-1 !== (err.stack || '').indexOf('Error: This is an error!'),
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
(err.stack || '').indexOf(
|
|
194
|
-
'YError: E_UNEXPECTED (This is an error!)',
|
|
195
|
-
),
|
|
196
|
-
'Stack contains bumped error.',
|
|
197
|
-
);
|
|
166
|
+
).toBeTruthy();
|
|
167
|
+
expect(
|
|
168
|
+
-1 !== (err.stack || '').indexOf('YError: E_UNEXPECTED ([])'),
|
|
169
|
+
).toBeTruthy();
|
|
198
170
|
}
|
|
199
|
-
|
|
171
|
+
expect(err.name).toEqual(err.toString());
|
|
200
172
|
});
|
|
201
173
|
|
|
202
174
|
test('Should work with YError like errors', () => {
|
|
203
175
|
const baseErr = new Error('E_A_NEW_ERROR');
|
|
204
176
|
|
|
205
177
|
(baseErr as YError).code = 'E_A_NEW_ERROR';
|
|
206
|
-
(baseErr as YError).
|
|
178
|
+
(baseErr as YError).debugValues = ['baseParam1', 'baseParam2'];
|
|
207
179
|
|
|
208
180
|
const err = YError.bump(baseErr);
|
|
209
181
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
182
|
+
expect(err.code).toEqual('E_A_NEW_ERROR');
|
|
183
|
+
expect(err.wrappedErrors.length).toEqual(1);
|
|
184
|
+
expect(err.debugValues).toEqual(['baseParam1', 'baseParam2']);
|
|
213
185
|
|
|
214
186
|
if ('captureStackTrace' in Error) {
|
|
215
|
-
|
|
187
|
+
expect(
|
|
216
188
|
-1 !== (err.stack || '').indexOf('Error: E_A_NEW_ERROR'),
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
assert(
|
|
189
|
+
).toBeTruthy();
|
|
190
|
+
expect(
|
|
220
191
|
-1 !==
|
|
221
192
|
(err.stack || '').indexOf(
|
|
222
|
-
'YError: E_A_NEW_ERROR (baseParam1,
|
|
193
|
+
'YError: E_A_NEW_ERROR (["baseParam1","baseParam2"])',
|
|
223
194
|
),
|
|
224
|
-
|
|
225
|
-
);
|
|
195
|
+
).toBeTruthy();
|
|
226
196
|
}
|
|
227
|
-
|
|
197
|
+
expect(err.name).toEqual(err.toString());
|
|
228
198
|
});
|
|
229
199
|
|
|
230
200
|
test('Should work with Y errors and a message', () => {
|
|
231
201
|
const err = YError.bump(
|
|
232
|
-
new YError('E_ERROR', 'arg1.1', 'arg1.2'),
|
|
202
|
+
new YError('E_ERROR', ['arg1.1', 'arg1.2']),
|
|
233
203
|
'E_ERROR_2',
|
|
234
|
-
'arg2.1',
|
|
235
|
-
'arg2.2',
|
|
204
|
+
['arg2.1', 'arg2.2'],
|
|
236
205
|
);
|
|
237
206
|
|
|
238
|
-
|
|
239
|
-
|
|
207
|
+
expect(err.code).toEqual('E_ERROR');
|
|
208
|
+
expect(err.debugValues).toEqual(['arg1.1', 'arg1.2']);
|
|
240
209
|
|
|
241
210
|
if ('captureStackTrace' in Error) {
|
|
242
|
-
|
|
243
|
-
-1 !==
|
|
244
|
-
|
|
245
|
-
);
|
|
246
|
-
|
|
247
|
-
-1 !==
|
|
248
|
-
|
|
249
|
-
);
|
|
211
|
+
expect(
|
|
212
|
+
-1 !==
|
|
213
|
+
(err.stack || '').indexOf('YError: E_ERROR (["arg1.1","arg1.2"])'),
|
|
214
|
+
).toBeTruthy();
|
|
215
|
+
expect(
|
|
216
|
+
-1 !==
|
|
217
|
+
(err.stack || '').indexOf('YError: E_ERROR (["arg1.1","arg1.2"])'),
|
|
218
|
+
).toBeTruthy();
|
|
250
219
|
}
|
|
251
|
-
|
|
220
|
+
expect(err.name).toEqual(err.toString());
|
|
252
221
|
});
|
|
253
222
|
});
|
|
254
223
|
});
|
package/src/index.ts
CHANGED
|
@@ -1,47 +1,35 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { EOL } from 'node:os';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
type YErrorParams = any;
|
|
3
|
+
export type YErrorDebugValue = unknown;
|
|
5
4
|
|
|
6
5
|
/**
|
|
7
|
-
* A YError class able to contain some
|
|
6
|
+
* A YError class able to contain some debugValues and
|
|
8
7
|
* print better stack traces
|
|
9
8
|
* @extends Error
|
|
10
9
|
*/
|
|
11
|
-
class YError extends Error {
|
|
10
|
+
class YError<T = YErrorDebugValue> extends Error {
|
|
12
11
|
code: string;
|
|
13
|
-
|
|
14
|
-
wrappedErrors: (Error | YError)[];
|
|
12
|
+
debugValues: T[] = [];
|
|
13
|
+
wrappedErrors: (Error | YError)[] = [];
|
|
15
14
|
/**
|
|
16
15
|
* Creates a new YError with an error code
|
|
17
|
-
* and some
|
|
16
|
+
* and some debugValues as debug values.
|
|
18
17
|
* @param {string} [errorCode = 'E_UNEXPECTED']
|
|
19
18
|
* The error code corresponding to the actual error
|
|
20
|
-
* @param {
|
|
19
|
+
* @param {any} [debugValues]
|
|
21
20
|
* Some additional debugging values
|
|
22
21
|
*/
|
|
23
|
-
constructor(errorCode: string, ...params: YErrorParams[]);
|
|
24
22
|
constructor(
|
|
25
|
-
wrappedErrors?: Error[],
|
|
26
23
|
errorCode?: string,
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
// Detecting if wrappedErrors are passed
|
|
31
|
-
if (!(wrappedErrors instanceof Array)) {
|
|
32
|
-
params = ('undefined' === typeof errorCode ? [] : [errorCode]).concat(
|
|
33
|
-
params,
|
|
34
|
-
);
|
|
35
|
-
errorCode = wrappedErrors;
|
|
36
|
-
wrappedErrors = [];
|
|
37
|
-
}
|
|
38
|
-
|
|
24
|
+
debugValues: T[] = [],
|
|
25
|
+
wrappedErrors: (Error | YError)[] = [],
|
|
26
|
+
) {
|
|
39
27
|
// Call the parent constructor
|
|
40
28
|
super(errorCode);
|
|
41
29
|
|
|
42
30
|
// Filling error
|
|
43
31
|
this.code = errorCode || 'E_UNEXPECTED';
|
|
44
|
-
this.
|
|
32
|
+
this.debugValues = debugValues;
|
|
45
33
|
this.wrappedErrors = wrappedErrors;
|
|
46
34
|
this.name = this.toString();
|
|
47
35
|
|
|
@@ -52,22 +40,22 @@ class YError extends Error {
|
|
|
52
40
|
|
|
53
41
|
/**
|
|
54
42
|
* Wraps any error and output a YError with an error
|
|
55
|
-
* code and some
|
|
43
|
+
* code and some debugValues as debug values.
|
|
56
44
|
* @param {Error} err
|
|
57
45
|
* The error to wrap
|
|
58
46
|
* @param {string} [errorCode = 'E_UNEXPECTED']
|
|
59
47
|
* The error code corresponding to the actual error
|
|
60
|
-
* @param {
|
|
48
|
+
* @param {any} [debugValues]
|
|
61
49
|
* Some additional debugging values
|
|
62
50
|
* @return {YError}
|
|
63
51
|
* The wrapped error
|
|
64
52
|
*/
|
|
65
|
-
static wrap<
|
|
66
|
-
err:
|
|
53
|
+
static wrap<T = YErrorDebugValue>(
|
|
54
|
+
err: Error | YError,
|
|
67
55
|
errorCode?: string,
|
|
68
|
-
|
|
56
|
+
debugValues: T[] = [],
|
|
69
57
|
): YError {
|
|
70
|
-
const wrappedErrorIsACode =
|
|
58
|
+
const wrappedErrorIsACode = looksLikeAYErrorCode(err.message);
|
|
71
59
|
const wrappedErrors = (
|
|
72
60
|
'wrappedErrors' in err ? err.wrappedErrors : []
|
|
73
61
|
).concat(err);
|
|
@@ -79,33 +67,31 @@ class YError extends Error {
|
|
|
79
67
|
errorCode = 'E_UNEXPECTED';
|
|
80
68
|
}
|
|
81
69
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
}
|
|
85
|
-
return new YError(wrappedErrors, errorCode, ...params);
|
|
70
|
+
|
|
71
|
+
return new YError(errorCode, debugValues, wrappedErrors);
|
|
86
72
|
}
|
|
87
73
|
|
|
88
74
|
/**
|
|
89
75
|
* Return a YError as is or wraps any other error and output
|
|
90
|
-
* a YError with a code and some
|
|
76
|
+
* a YError with a code and some debugValues as debug values.
|
|
91
77
|
* @param {Error} err
|
|
92
78
|
* The error to cast
|
|
93
79
|
* @param {string} [errorCode = 'E_UNEXPECTED']
|
|
94
80
|
* The error code corresponding to the actual error
|
|
95
|
-
* @param {
|
|
81
|
+
* @param {any} [debugValues]
|
|
96
82
|
* Some additional debugging values
|
|
97
83
|
* @return {YError}
|
|
98
84
|
* The wrapped error
|
|
99
85
|
*/
|
|
100
|
-
static cast<
|
|
101
|
-
err:
|
|
86
|
+
static cast<T = YErrorDebugValue>(
|
|
87
|
+
err: Error | YError,
|
|
102
88
|
errorCode?: string,
|
|
103
|
-
|
|
89
|
+
debugValues: T[] = [],
|
|
104
90
|
): YError {
|
|
105
|
-
if (
|
|
106
|
-
return err
|
|
91
|
+
if (looksLikeAYError(err)) {
|
|
92
|
+
return err;
|
|
107
93
|
}
|
|
108
|
-
return YError.wrap(err, errorCode,
|
|
94
|
+
return YError.wrap(err, errorCode, debugValues);
|
|
109
95
|
}
|
|
110
96
|
|
|
111
97
|
/**
|
|
@@ -116,40 +102,41 @@ class YError extends Error {
|
|
|
116
102
|
* The error to bump
|
|
117
103
|
* @param {string} [errorCode = 'E_UNEXPECTED']
|
|
118
104
|
* The error code corresponding to the actual error
|
|
119
|
-
* @param {
|
|
105
|
+
* @param {any} [debugValues]
|
|
120
106
|
* Some additional debugging values
|
|
121
107
|
* @return {YError}
|
|
122
108
|
* The wrapped error
|
|
123
109
|
*/
|
|
124
|
-
static bump<
|
|
125
|
-
err:
|
|
110
|
+
static bump<T = YErrorDebugValue>(
|
|
111
|
+
err: Error | YError,
|
|
126
112
|
errorCode?: string,
|
|
127
|
-
|
|
113
|
+
debugValues: T[] = [],
|
|
128
114
|
): YError {
|
|
129
|
-
if (
|
|
130
|
-
return YError.wrap(err,
|
|
115
|
+
if (looksLikeAYError(err)) {
|
|
116
|
+
return YError.wrap(err, err.code, err.debugValues);
|
|
131
117
|
}
|
|
132
|
-
return YError.wrap(err, errorCode,
|
|
118
|
+
return YError.wrap(err, errorCode, debugValues);
|
|
133
119
|
}
|
|
134
120
|
|
|
135
121
|
toString(): string {
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
this.
|
|
146
|
-
|
|
147
|
-
|
|
122
|
+
let debugValuesAsString: string;
|
|
123
|
+
|
|
124
|
+
try {
|
|
125
|
+
debugValuesAsString = JSON.stringify(this.debugValues);
|
|
126
|
+
} catch {
|
|
127
|
+
debugValuesAsString = '<circular>';
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return `${
|
|
131
|
+
this.wrappedErrors.length
|
|
132
|
+
? this.wrappedErrors[this.wrappedErrors.length - 1].stack + EOL
|
|
133
|
+
: ''
|
|
134
|
+
}${this.constructor.name}: ${this.code} (${debugValuesAsString})`;
|
|
148
135
|
}
|
|
149
136
|
}
|
|
150
137
|
|
|
151
138
|
/**
|
|
152
|
-
* Allow to print a stack from anything (especially
|
|
139
|
+
* Allow to print a stack from anything (especially caught
|
|
153
140
|
* errors that may or may not contain errors 🤷).
|
|
154
141
|
* @param {Error} err
|
|
155
142
|
* The error to print
|
|
@@ -167,8 +154,8 @@ export function printStackTrace(err: Error | YError): string {
|
|
|
167
154
|
}
|
|
168
155
|
|
|
169
156
|
// In order to keep compatibility through major versions
|
|
170
|
-
// we have to make kind of
|
|
171
|
-
function
|
|
157
|
+
// we have to make kind of a cross major version instanceof
|
|
158
|
+
export function looksLikeAYError(err: Error | YError): err is YError {
|
|
172
159
|
return (
|
|
173
160
|
!!(err instanceof YError) ||
|
|
174
161
|
!!(
|
|
@@ -177,15 +164,15 @@ function _looksLikeAYError(err: Error | YError): boolean {
|
|
|
177
164
|
err.constructor.name.endsWith('Error') &&
|
|
178
165
|
'code' in err &&
|
|
179
166
|
'string' === typeof err.code &&
|
|
180
|
-
|
|
181
|
-
'
|
|
182
|
-
err.
|
|
183
|
-
err.
|
|
167
|
+
looksLikeAYErrorCode(err.code) &&
|
|
168
|
+
'debugValues' in err &&
|
|
169
|
+
err.debugValues &&
|
|
170
|
+
err.debugValues instanceof Array
|
|
184
171
|
)
|
|
185
172
|
);
|
|
186
173
|
}
|
|
187
174
|
|
|
188
|
-
function
|
|
175
|
+
export function looksLikeAYErrorCode(str: string): boolean {
|
|
189
176
|
return /^([A-Z0-9_]+)$/.test(str);
|
|
190
177
|
}
|
|
191
178
|
|