44 */
55
66import AdvanceStringIndex from 'es-abstract/2021/AdvanceStringIndex'
7+ import CreateProperty from 'es-abstract/2021/CreateDataProperty'
78import CreatePropertyOrThrow from 'es-abstract/2021/CreateDataPropertyOrThrow'
89import GetMethod from 'es-abstract/2021/GetMethod'
910import IsArray from 'es-abstract/2021/IsArray'
1011import IterableToList from 'es-abstract/2021/IterableToList'
1112import Type from 'es-abstract/2021/Type'
1213import getIteratorMethod from 'es-abstract/helpers/getIteratorMethod'
14+ import type Options from './options'
15+ import type GetIteratorMethodOptions from './options-get-iterator-method'
1316
1417/**
15- * Iterator method options type.
18+ * The `AggregateError` object represents an error when several errors need to
19+ * be wrapped in a single error.
1620 *
17- * @see {@link getIteratorMethod }
18- */
19- type GetIteratorMethodOptions = Parameters < typeof getIteratorMethod > [ '0' ]
20-
21- /**
22- * A single error that represents a group of errors.
23- *
24- * It is thrown when multiple errors need to be reported by an operation, e.g.
25- * by [`Promise.any()`][1] when all promises passed to it reject.
21+ * It is thrown when multiple errors need to be reported by an operation, for
22+ * example by [`Promise.any()`][1] when all promises passed to it reject.
2623 *
2724 * [1]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise/any
2825 *
29- * @see https://tc39.es/proposal-promise-any#sec-aggregate-error-objects
26+ * @see https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/AggregateError
3027 *
3128 * @template T - Aggregated error type
29+ * @template C - Error cause type
3230 *
31+ * @class
3332 * @extends {Error }
3433 */
35- class AggregateError < T = any > extends Error {
34+ class AggregateError < T = any , C = unknown > extends Error {
35+ /**
36+ * Error cause.
37+ *
38+ * @see https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error/cause
39+ *
40+ * @public
41+ * @member {C | undefined} cause
42+ */
43+ public cause ?: C
44+
3645 /**
3746 * @public
38- * @member {T[]} errors - Aggregated errors
47+ * @member {T[]} errors - Array containing aggregated errors
3948 */
40- public errors : T [ ]
49+ public errors : T [ ] = [ ]
4150
4251 /**
4352 * @public
4453 * @override
4554 * @readonly
46- * @member {'AggregateError' } name - Error subclass name
55+ * @member {string } name - Error name
4756 */
48- public override readonly name : 'AggregateError' = 'AggregateError'
57+ public override readonly name : string = 'AggregateError'
4958
5059 /**
51- * Creates a single error representing `errors`.
60+ * Creates an error for several errors that need to be wrapped in a single
61+ * error.
5262 *
5363 * @example
5464 * new AggregateError([new Error('some error')])
65+ * @example
5566 * new AggregateError([new Error('err1'), new Error('err2')], 'oh no!')
5667 *
57- * @param {Iterable<T> } errors - Aggregated errors
58- * @param {string } [message] - Human-readable error message
68+ * @param {Iterable<T> } errors - An iterable of errors
69+ * @param {string } [message] - Human-readable description of the error
70+ * @param {Options<C> } [options] - Error options
71+ * @param {C } [options.cause] - The original cause of the error
5972 */
60- constructor ( errors : Iterable < T > , message ?: string ) {
73+ constructor ( errors : Iterable < T > , message ?: string , options ?: Options < C > ) {
6174 super ( message )
6275
6376 /**
64- * Iterator method options.
77+ * Iterator method creation options.
6578 *
6679 * @const {GetIteratorMethodOptions} es
6780 */
@@ -73,17 +86,14 @@ class AggregateError<T = any> extends Error {
7386 }
7487
7588 /**
76- * Function that returns an iterator .
89+ * Array containing aggregated errors .
7790 *
78- * @const {() => Iterator<T>} method
91+ * @const {T[]} arr
7992 */
80- const method : ( ) => Iterator < T > = getIteratorMethod ( es , errors )
81-
82- // create aggregated error list
83- this . errors = IterableToList ( errors , method )
93+ const arr : T [ ] = IterableToList ( errors , getIteratorMethod ( es , errors ) )
8494
85- // re-assign errors property or throw if errors isn't iterable
86- CreatePropertyOrThrow ( this , 'errors ' , IterableToList ( this . errors , method ) )
95+ CreatePropertyOrThrow ( this , ' errors' , arr )
96+ CreateProperty ( this , 'cause ' , options ?. cause )
8797 }
8898}
8999
0 commit comments