-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy patherror-constructors.js
42 lines (36 loc) · 1.05 KB
/
error-constructors.js
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
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 function addKnownErrorConstructor(constructor) {
const {name} = constructor;
if (errorConstructors.has(name)) {
throw new Error(`The error constructor "${name}" is already known.`);
}
try {
// eslint-disable-next-line no-new -- It just needs to be verified
new constructor();
} catch (error) {
throw new Error(`The error constructor "${name}" is not compatible`, {cause: error});
}
errorConstructors.set(name, constructor);
}