The AggregateError object represents an error when several errors need to be wrapped in a single error.
Syntax
new AggregateError(errors[, message])
Parameters
errors- An iterable of errors, may not actually be
Errorinstances. messageOptional- An optional human-readable description of the aggregate error.
Description
An AggregateError is thrown when multiple errors need to be reported by an operation, for example by Promise.any(), when all promises passed to it reject.
Instance properties
AggregateError.prototype.message- Error message, defaults to
"". AggregateError.prototype.name- Error name, defaults to
"AggregateError". AggregateError.prototype.errors- The
Arrayof errors which caused thisAggregateErrorto be thrown. Each access to this property causes a newArrayto be created.
Examples
Catching an AggregateError
Promise.any([
Promise.reject(new Error("some error")),
]).catch(e => {
console.log(e instanceof AggregateError); // true
console.log(e.message); // "All Promises rejected"
console.log(e.name); // "AggregateError"
console.log(e.errors); // [ Error: "some error" ]
});
Creating an AggregateError
try {
throw new AggregateError([
new Error("some error"),
], 'Hello');
} catch (e) {
console.log(e instanceof AggregateError); // true
console.log(e.message); // "Hello"
console.log(e.name); // "AggregateError"
console.log(e.errors); // [ Error: "some error" ]
}
Specifications
| Specification | Status | Comment |
|---|---|---|
| ESNext Promise.any Proposal | Stage 3 Draft | Initial definition. |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
No compatibility data found. Please contribute data for "javascript.builtins.AggregateError" (depth: 1) to the MDN compatibility data repository.