AggregateError
Baseline 广泛可用 *
AggregateError 对象表示当需要将多个错误封装到单个错误中时产生的错误。当某个操作需要报告多个错误时,例如 Promise.any() 在其传入的所有 Promise 都被拒绝时,就会抛出它。
AggregateError 是 Error 的一个子类。
构造函数
AggregateError()-
创建一个新的
AggregateError对象。
实例属性
还继承了其父级 Error 的实例属性。.
这些属性定义在 AggregateError.prototype 上,并被所有 AggregateError 实例共享。
AggregateError.prototype.constructor-
创建了实例对象的构造函数。对于
AggregateError实例,初始值为AggregateError构造函数。 AggregateError.prototype.name-
表示错误类型的名称。对于
AggregateError.prototype.name,初始值为"AggregateError"。
这些属性是每个 AggregateError 实例的自有属性。
errors-
一个数组,表示被聚合的错误。
实例方法
继承了其父级 Error 的实例方法。.
示例
捕获 AggregateError
js
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
js
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" ]
}
规范
| 规范 |
|---|
| ECMAScript® 2026 语言规范 # sec-aggregate-error-objects |
浏览器兼容性
加载中…