
昨天我们学习了关于"深入了解JavaScript标准内置对象:JSON.stringify()",今天学习另外一个JavaScript标准内置对象:AggregateError。
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" ]
}
51工具盒子