-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathponyfill.ts
More file actions
100 lines (91 loc) · 2.9 KB
/
Copy pathponyfill.ts
File metadata and controls
100 lines (91 loc) · 2.9 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
* @file AggregateError
* @module aggregate-error-ponyfill/AggregateError
*/
import AdvanceStringIndex from 'es-abstract/2021/AdvanceStringIndex'
import CreateProperty from 'es-abstract/2021/CreateDataProperty'
import CreatePropertyOrThrow from 'es-abstract/2021/CreateDataPropertyOrThrow'
import GetMethod from 'es-abstract/2021/GetMethod'
import IsArray from 'es-abstract/2021/IsArray'
import IterableToList from 'es-abstract/2021/IterableToList'
import Type from 'es-abstract/2021/Type'
import getIteratorMethod from 'es-abstract/helpers/getIteratorMethod'
import type Options from './options'
import type GetIteratorMethodOptions from './options-get-iterator-method'
/**
* The `AggregateError` object represents an error when several errors need to
* be wrapped in a single error.
*
* It is thrown when multiple errors need to be reported by an operation, for
* example by [`Promise.any()`][1] when all promises passed to it reject.
*
* [1]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise/any
*
* @see https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/AggregateError
*
* @template T - Aggregated error type
* @template Cause - Error cause type
*
* @class
* @extends {Error}
*/
class AggregateError<T = any, Cause = unknown> extends Error {
/**
* Error cause.
*
* @see https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error/cause
*
* @public
* @member {Cause | undefined} cause
*/
public cause?: Cause | undefined
/**
* @public
* @member {T[]} errors - Array containing aggregated errors
*/
public errors: T[] = []
/**
* @public
* @override
* @readonly
* @member {string} name - Error name
*/
public override readonly name: string = 'AggregateError'
/**
* Creates an error for several errors that need to be wrapped in a single
* error.
*
* @example
* new AggregateError([new Error('some error')])
* @example
* new AggregateError([new Error('err1'), new Error('err2')], 'oh no!')
*
* @param {Iterable<T>} errors - An iterable of errors
* @param {string} [message] - Human-readable description of the error
* @param {Options<Cause>} [options] - Error options
* @param {Cause} [options.cause] - The original cause of the error
*/
constructor(errors: Iterable<T>, message?: string, options?: Options<Cause>) {
super(message)
/**
* Iterator method creation options.
*
* @const {GetIteratorMethodOptions} es
*/
const es: GetIteratorMethodOptions = {
AdvanceStringIndex,
GetMethod,
IsArray,
Type
}
/**
* Array containing aggregated errors.
*
* @const {T[]} arr
*/
const arr: T[] = IterableToList(errors, getIteratorMethod(es, errors))
CreatePropertyOrThrow(this, 'errors', arr)
CreateProperty(this, 'cause', options?.cause)
}
}
export default AggregateError