-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.d.ts
78 lines (75 loc) · 2.41 KB
/
main.d.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
/**
* `normalize-exception` options
*/
export interface Options {
/**
* Unless `true`,
* [`error.cause`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause)
* and
* [`error.errors`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError)
* are normalized recursively, when present.
*/
readonly shallow?: boolean
}
type DefinedString<Value, DefaultValue> = Value extends string
? Value extends ''
? DefaultValue
: Value
: DefaultValue
type NormalizedError<ErrorArg> = Error &
(ErrorArg extends Error
? {
name: DefinedString<ErrorArg['name'], Error['constructor']['name']>
message: DefinedString<ErrorArg['message'], ''>
stack: DefinedString<ErrorArg['stack'], string>
}
: unknown)
/**
* Normalize exception/error.
*
* In JavaScript, one can `throw` any value including strings, objects (like
* `{ message: "..." }`) or even `undefined`. This normalizes any exception
* to an `Error` instance.
*
* It also fixes any missing or invalid error properties: `name`, `message`,
* `stack`, `cause`, `errors`.
*
* @example
* ```js
* import normalizeException from 'normalize-exception'
*
* try {
* throw null
* } catch (error) {
* const normalizedError = normalizeException(error)
* console.log(normalizedError) // Error: null
* // Without `normalizeException()`, this would throw
* console.log(normalizedError.name)
* }
*
* console.log(normalizeException('message')) // Error: message
*
* console.log(normalizeException({ name: 'TypeError', message: 'message' })) // TypeError: message
*
* const error = new TypeError('message')
* console.log(error.stack) // TypeError: message
*
* // `error.stack` is cached, so it does not update
* error.message += ' otherMessage'
* console.log(error.stack) // TypeError: message
*
* const normalizedError = normalizeException(error)
* console.log(normalizedError.stack) // TypeError: message otherMessage
*
* const error = new Error('message', { cause: 'innerError' })
* console.log(error.cause instanceof Error) // false
*
* const normalizedError = normalizeException(error)
* console.log(normalizedError.cause instanceof Error) // true
* console.log(normalizedError.cause) // Error: innerError
* ```
*/
export default function normalizeException<ErrorArg>(
error: ErrorArg,
options?: Options,
): NormalizedError<ErrorArg>