forked from MetaMask/utils
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherrors.test.ts
322 lines (242 loc) · 9.83 KB
/
errors.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import fs from 'fs';
import {
getErrorMessage,
isErrorWithCode,
isErrorWithMessage,
isErrorWithStack,
wrapError,
} from './errors';
describe('isErrorWithCode', () => {
it('returns true if given an object that includes a "code" property', () => {
expect(
isErrorWithCode({ code: 'some code', message: 'some message' }),
).toBe(true);
});
it('returns false if given null', () => {
expect(isErrorWithCode(null)).toBe(false);
});
it('returns false if given undefined', () => {
expect(isErrorWithCode(undefined)).toBe(false);
});
it('returns false if given something that is not typeof object', () => {
expect(isErrorWithCode(12345)).toBe(false);
});
it('returns false if given an empty object', () => {
expect(isErrorWithCode({})).toBe(false);
});
it('returns false if given a non-empty object that does not have a "code" property', () => {
expect(isErrorWithCode({ message: 'some message' })).toBe(false);
});
});
describe('isErrorWithMessage', () => {
it('returns true if given an object that includes a "message" property', () => {
expect(
isErrorWithMessage({ code: 'some code', message: 'some message' }),
).toBe(true);
});
it('returns false if given null', () => {
expect(isErrorWithMessage(null)).toBe(false);
});
it('returns false if given undefined', () => {
expect(isErrorWithMessage(undefined)).toBe(false);
});
it('returns false if given something that is not typeof object', () => {
expect(isErrorWithMessage(12345)).toBe(false);
});
it('returns false if given an empty object', () => {
expect(isErrorWithMessage({})).toBe(false);
});
it('returns false if given a non-empty object that does not have a "message" property', () => {
expect(isErrorWithMessage({ code: 'some code' })).toBe(false);
});
});
describe('isErrorWithStack', () => {
it('returns true if given an object that includes a "stack" property', () => {
expect(isErrorWithStack({ code: 'some code', stack: 'some stack' })).toBe(
true,
);
});
it('returns false if given null', () => {
expect(isErrorWithStack(null)).toBe(false);
});
it('returns false if given undefined', () => {
expect(isErrorWithStack(undefined)).toBe(false);
});
it('returns false if given something that is not typeof object', () => {
expect(isErrorWithStack(12345)).toBe(false);
});
it('returns false if given an empty object', () => {
expect(isErrorWithStack({})).toBe(false);
});
it('returns false if given a non-empty object that does not have a "stack" property', () => {
expect(
isErrorWithStack({ code: 'some code', message: 'some message' }),
).toBe(false);
});
});
describe('wrapError', () => {
describe('if the original error is an Error instance not generated by fs.promises', () => {
it('returns a new Error with the given message', () => {
const originalError = new Error('oops');
const newError = wrapError(originalError, 'Some message');
expect(newError.message).toBe('Some message');
});
it('links to the original error via "cause"', () => {
const originalError = new Error('oops');
const newError = wrapError(originalError, 'Some message');
expect(newError.cause).toBe(originalError);
});
it('copies over any "code" property that exists on the given Error', () => {
const originalError = new Error('oops');
// @ts-expect-error The Error interface doesn't have a "code" property
originalError.code = 'CODE';
const newError = wrapError(originalError, 'Some message');
expect(newError.code).toBe('CODE');
});
});
describe('if the original error was generated by fs.promises', () => {
it('returns a new Error with the given message', async () => {
let originalError;
try {
await fs.promises.readFile('/tmp/nonexistent', 'utf8');
} catch (error: any) {
originalError = error;
}
const newError = wrapError(originalError, 'Some message');
expect(newError.message).toBe('Some message');
});
it("links to the original error via 'cause'", async () => {
let originalError;
try {
await fs.promises.readFile('/tmp/nonexistent', 'utf8');
} catch (error: any) {
originalError = error;
}
const newError = wrapError(originalError, 'Some message');
expect(newError.cause).toBe(originalError);
});
it('copies over any "code" property that exists on the given Error', async () => {
let originalError;
try {
await fs.promises.readFile('/tmp/nonexistent', 'utf8');
} catch (error: any) {
originalError = error;
}
const newError = wrapError(originalError, 'Some message');
expect(newError.code).toBe('ENOENT');
});
});
describe('if the original error has a length === 2', () => {
it('returns an error with cause', () => {
// Save original Error
const OriginalError = global.Error;
// Create a mock error with a cause
class MockError extends Error {
constructor(message: string, options: { cause?: Error } = {}) {
super(message);
this.cause = options.cause;
}
cause: Error | undefined;
}
// Set length to 2
Object.defineProperty(MockError, 'length', { value: 2 });
// Replace global Error with MockError
// NOTE: when we upgrade jest, change this to use:
// jest.replaceProperty(global, 'Error', MockError);
global.Error = MockError as unknown as ErrorConstructor;
// Define your original error and message
const originalError = new Error('original error');
const message = 'new error message';
// Call your function
const result = wrapError(originalError, message);
// Assert that the error has the expected properties
expect(result.message).toBe(message);
expect(result.cause).toBe(originalError);
// Restore the original Error constructor
global.Error = OriginalError;
});
});
describe('if the original error is an object but not an Error instance', () => {
describe('if the message is a non-empty string', () => {
it('combines a string version of the original error and message together in a new Error', () => {
const originalError = { some: 'error' };
const newError = wrapError(originalError, 'Some message');
expect(newError.message).toBe('[object Object]: Some message');
});
it('does not set a cause on the new Error', async () => {
const originalError = { some: 'error' };
const newError = wrapError(originalError, 'Some message');
expect(newError.cause).toBeUndefined();
});
it('does not set a code on the new Error', async () => {
const originalError = { some: 'error' };
const newError = wrapError(originalError, 'Some message');
expect(newError.code).toBeUndefined();
});
});
describe('if the message is an empty string', () => {
it('places a string version of the original error in a new Error object without an additional message', () => {
const originalError = { some: 'error' };
const newError = wrapError(originalError, '');
expect(newError.message).toBe('[object Object]');
});
it('does not set a cause on the new Error', async () => {
const originalError = { some: 'error' };
const newError = wrapError(originalError, '');
expect(newError.cause).toBeUndefined();
});
it('does not set a code on the new Error', async () => {
const originalError = { some: 'error' };
const newError = wrapError(originalError, '');
expect(newError.code).toBeUndefined();
});
});
});
describe('if the original error is a string', () => {
describe('if the message is a non-empty string', () => {
it('combines the original error and message together in a new Error', () => {
const newError = wrapError('Some original message', 'Some message');
expect(newError.message).toBe('Some original message: Some message');
});
it('does not set a cause on the new Error', () => {
const newError = wrapError('Some original message', 'Some message');
expect(newError.cause).toBeUndefined();
});
it('does not set a code on the new Error', () => {
const newError = wrapError('Some original message', 'Some message');
expect(newError.code).toBeUndefined();
});
});
describe('if the message is an empty string', () => {
it('places the original error in a new Error object without an additional message', () => {
const newError = wrapError('Some original message', '');
expect(newError.message).toBe('Some original message');
});
it('does not set a cause on the new Error', () => {
const newError = wrapError('Some original message', '');
expect(newError.cause).toBeUndefined();
});
it('does not set a code on the new Error', () => {
const newError = wrapError('Some original message', '');
expect(newError.code).toBeUndefined();
});
});
});
});
describe('getErrorMessage', () => {
it("returns the value of the 'message' property from the given object if it is present", () => {
expect(getErrorMessage({ message: 'hello' })).toBe('hello');
});
it("returns the result of calling .toString() on the given object if it has no 'message' property", () => {
expect(getErrorMessage({ foo: 'bar' })).toBe('[object Object]');
});
it('returns the result of calling .toString() on the given non-object', () => {
expect(getErrorMessage(42)).toBe('42');
});
it('returns an empty string if given null', () => {
expect(getErrorMessage(null)).toBe('');
});
it('returns an empty string if given undefined', () => {
expect(getErrorMessage(undefined)).toBe('');
});
});