AggregateError() 构造函数创建了一个包装了多个错误对象的单个错误对象。
语法
new AggregateError(errors)
new AggregateError(errors, message)
new AggregateError(errors, message, options)
AggregateError(errors)
AggregateError(errors, message)
AggregateError(errors, message, options)
[!NOTE] 调用
AggregateError()可以使用或不使用new,两种方法都会创建一个新的AggregateError实例。
参数
errors- : 一系列错误对象,实际上可能不是 Error 的实例。
message- : 一个可选的对错误集合的可读描述。
options- : 一个具有以下属性的对象:
cause- : 表示错误的具体原因的属性。当捕捉并重新抛出一个带有更具体或有用的错误信息的错误时,这个属性可以用来传递原始错误。
- : 一个具有以下属性的对象:
示例
创建 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" ]
}
规范
浏览器兼容性
参见
core-js中AggregateError的 polyfill- Promise.any
来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。
