-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
41 lines (37 loc) · 1.05 KB
/
index.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
// @ts-expect-error
import formatter from 'format'
export const fault = Object.assign(create(Error), {
eval: create(EvalError),
range: create(RangeError),
reference: create(ReferenceError),
syntax: create(SyntaxError),
type: create(TypeError),
uri: create(URIError)
})
/**
* Create a new `EConstructor`, with the formatted `format` as a first argument.
*
* @template {Error} Fault
* @template {new (reason: string) => Fault} Class
* @param {Class} Constructor
*/
export function create(Constructor) {
/** @type {string} */
// @ts-expect-error
FormattedError.displayName = Constructor.displayName || Constructor.name
return FormattedError
/**
* Create an error with a printf-like formatted message.
*
* @param {string|null} [format]
* Template string.
* @param {...unknown} values
* Values to render in `format`.
* @returns {Fault}
*/
function FormattedError(format, ...values) {
/** @type {string} */
const reason = format ? formatter(format, ...values) : format
return new Constructor(reason)
}
}