×

JavaScript标准内置对象:AggregateError

作者:Terry2020.12.15来源:Web前端之家浏览:8855评论:0
关键词:AggregateErrorjs

500.jpg

昨天我们学习了关于“深入了解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" ]
}

您的支持是我们创作的动力!
温馨提示:本文作者系Terry ,经Web前端之家编辑修改或补充,转载请注明出处和本文链接:
https://jiangweishan.com/article/jsAggregateError20201215.html

网友评论文明上网理性发言 已有0人参与

发表评论: