此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

AggregateError() 构造函数

基线 广泛可用

自 2020年9月 起,此特性已在主流浏览器中得到支持,可在大多数设备和浏览器版本中正常使用。

AggregateError() 构造函数创建了一个包装了多个错误对象的单个错误对象。

语法

js
new AggregateError(errors)
new AggregateError(errors, message)
new AggregateError(errors, message, options)

AggregateError(errors)
AggregateError(errors, message)
AggregateError(errors, message, options)

备注:调用 AggregateError() 可以使用或不使用 new,两种方法都会创建一个新的 AggregateError 实例。

参数

errors

一系列错误对象,实际上可能不是 Error 的实例。

message 可选

一个可选的对错误集合的可读描述。

options 可选

一个具有以下属性的对象:

cause 可选

表示错误的具体原因的属性。当捕捉并重新抛出一个带有更具体或有用的错误信息的错误时,这个属性可以用来传递原始错误。

示例

创建 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® 2027 Language Specification
# sec-aggregate-error-constructor

浏览器兼容性

参见