此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。
AggregateError
Baseline Widely available *
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2020年9月.
* Some parts of this feature may have varying levels of support.
AggregateError 对象代表了包装了多个错误对象的单个错误对象。当一个操作需要报告多个错误时,例如Promise.any(),当传递给它的所有承诺都被拒绝时,就会抛出该错误。
AggregateError 是Error 的子类。
In this article
构造函数
AggregateError()创建一个新的
AggregateError对象
实例属性
从父类Error 中继承实例属性。
以下属性在AggregateError.prototype 上定义,并由所有AggregateError 实例共享。
AggregateError.prototype.constructor创建实例对象的构造函数。对于
AggregateError实例来说,初始值为AggregateError构造函数。AggregateError.prototype.name代表了错误类型的名称,对于
AggregateError.prototype.name来说,初始值为"AggregateError"。
这些属性是每个AggregateError 实例的自有属性。
errors一个数组,基本上反映了
AggregateError实例化时使用的迭代器;例如,如果AggregateError是用AggregateError()构造函数创建的,则作为第一个参数传递给构造函数的任何迭代器生成的数组。
实例方法
从父类Error 中继承实例方法。
示例
>捕获一个 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" ]});创建一个 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" ]}规范
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-aggregate-error-objects> |