AggregateError 对象代表了包装了多个错误对象的单个错误对象。当一个操作需要报告多个错误时,例如 {{JSxRef("Promise.any()")}},当传递给它的所有承诺都被拒绝时,就会抛出该错误。
AggregateError 是 Error 的子类。
构造函数
- {{jsxref("Global_Objects/AggregateError/AggregateError", "AggregateError()")}}
- : 创建一个新的
AggregateError对象
- : 创建一个新的
实例属性
从父类 Error 中继承实例属性。
以下属性在 AggregateError.prototype 上定义,并由所有 AggregateError 实例共享。
- {{jsxref("Object/constructor", "AggregateError.prototype.constructor")}}
- : 创建实例对象的构造函数。对于
AggregateError实例来说,初始值为 {{jsxref("AggregateError/AggregateError", "AggregateError")}} 构造函数。
- : 创建实例对象的构造函数。对于
- {{jsxref("Error/name", "AggregateError.prototype.name")}}
- : 代表了错误类型的名称,对于
AggregateError.prototype.name来说,初始值为"AggregateError"。
- : 代表了错误类型的名称,对于
这些属性是每个 AggregateError 实例的自有属性。
errors- : 一个数组,基本上反映了
AggregateError实例化时使用的迭代器;例如,如果AggregateError是用 {{JSxRef("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" ]
}