AggregateError
Experimental: 这是一个实验中的功能
此功能某些浏览器尚在开发中,请参考浏览器兼容性表格以得到在不同浏览器中适合使用的前缀。由于该功能对应的标准文档可能被重新修订,所以在未来版本的浏览器中该功能的语法和行为可能随之改变。
**AggregateError
**当多个错误 需要包装在一个错误中时,该对象表示一个错误。
语法
new AggregateError(errors[, message])
参数
errors
-
错误的描述,默认为空。
message
可选-
AggregateError 错误的提示信息。
描述
一个AggregateError
当需要由操作报告多个错误被抛出,例如通过Promise.any()
,在传递给它的所有Promise
拒绝。
属性
AggregateError.prototype
-
AggregateError
的原型
AggregateError 实例
实例属性
AggregateError.prototype.constructor
-
指定创建实例原型的函数。
AggregateError.prototype.message
-
错误消息,默认为
""
。 AggregateError.prototype.name
-
错误名称,默认为
"AggregateError"
。
示例
捕获一个 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 Language Specification # sec-aggregate-error-objects |
浏览器兼容性
BCD tables only load in the browser