forked from sindresorhus/serialize-error
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror-constructors.js
More file actions
70 lines (58 loc) · 1.76 KB
/
error-constructors.js
File metadata and controls
70 lines (58 loc) · 1.76 KB
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
const list = [
// Native ES errors https://262.ecma-international.org/12.0/#sec-well-known-intrinsic-objects
Error,
EvalError,
RangeError,
ReferenceError,
SyntaxError,
TypeError,
URIError,
AggregateError,
// Built-in errors
globalThis.DOMException,
// Node-specific errors
// https://nodejs.org/api/errors.html
globalThis.AssertionError,
globalThis.SystemError,
]
// Non-native Errors are used with `globalThis` because they might be missing. This filter drops them when undefined.
.filter(Boolean)
.map(constructor => [constructor.name, constructor]);
export const errorConstructors = new Map(list);
export const errorFactories = new Map();
export function addKnownErrorConstructor(constructor, factory) {
let instance;
let resolvedName;
if (factory) {
if (typeof factory !== 'function') {
throw new TypeError('Factory must be a function');
}
// Verify factory can execute without throwing
try {
instance = factory();
} catch (error) {
throw new Error('Factory is not compatible', {cause: error});
}
if (!(instance instanceof constructor)) {
throw new TypeError('Factory must return an instance of the constructor');
}
resolvedName = instance.name;
} else {
try {
instance = new constructor();
} catch (error) {
throw new Error(`Constructor "${constructor.name}" is not compatible`, {cause: error});
}
resolvedName = instance.name;
}
if (!resolvedName || typeof resolvedName !== 'string') {
throw new TypeError('Error instances must have a non-empty string "name" property');
}
if (errorConstructors.has(resolvedName)) {
throw new Error(`Error constructor "${resolvedName}" is already known`);
}
errorConstructors.set(resolvedName, constructor);
if (factory) {
errorFactories.set(resolvedName, factory);
}
}