-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.d.ts
73 lines (69 loc) · 2.27 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
import type setErrorProps from 'set-error-props'
type NormalizeError<ErrorArg> = ErrorArg extends Error ? ErrorArg : Error
type SetErrorProps<ErrorArg extends object, Cause extends object> = ReturnType<
typeof setErrorProps<ErrorArg, Cause, { soft: true }>
>
type MergeErrorCause<ErrorArg> = 'cause' extends keyof ErrorArg
? Omit<
ErrorArg['cause'] extends object
? SetErrorProps<NormalizeError<ErrorArg>, ErrorArg['cause']>
: NormalizeError<ErrorArg>,
'cause'
> &
Error
: NormalizeError<ErrorArg>
/**
* This merges
* [`error.cause`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause)
* recursively with its parent `error`, including its
* [`message`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/message),
* [`stack`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/stack),
* [`name`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/name)
* and
* [`errors`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError).
*
* `error` is modified and returned.
*
* If `error`'s class is `Error` or if `error.wrap` is `true`,
* [`error.cause`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause)
* is modified and returned instead.
*
* If `error` is not a valid `Error`, a new `error` is created and returned
* instead.
*
* This never throws.
*
* @example
* ```js
* const main = (userId) => {
* try {
* return createUser(userId)
* } catch (error) {
* throw mergeErrorCause(error)
* // Printed as:
* // TypeError: Invalid user id: false
* // Could not create user.
* }
* }
*
* const createUser = (userId) => {
* try {
* validateUserId(userId)
* return sendDatabaseRequest('create', userId)
* } catch (cause) {
* throw new Error('Could not create user.', { cause })
* }
* }
*
* const validateUserId = (userId) => {
* if (typeof userId !== 'string') {
* throw new TypeError(`Invalid user id: ${userId}.`)
* }
* }
*
* main(false)
* ```
*/
export default function mergeErrorCause<ErrorArg>(
error: ErrorArg,
): MergeErrorCause<ErrorArg>