AggregateError

Baseline Widely available *

This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2020.

* Some parts of this feature may have varying levels of support.

AggregateError 对象代表了包装了多个错误对象的单个错误对象。当一个操作需要报告多个错误时,例如 Promise.any(),当传递给它的所有承诺都被拒绝时,就会抛出该错误。

AggregateErrorError 的子类。

构造函数

AggregateError()

创建一个新的 AggregateError 对象

实例属性

从父类 Error 中继承实例属性。

以下属性在 AggregateError.prototype 上定义,并由所有 AggregateError 实例共享。

AggregateError.prototype.constructor

创建实例对象的构造函数。对于 AggregateError 实例来说,初始值为 AggregateError 构造函数。

AggregateError.prototype.name

代表了错误类型的名称,对于 AggregateError.prototype.name 来说,初始值为 "AggregateError"

这些属性是每个 AggregateError 实例的自有属性。

errors

一个数组,基本上反映了 AggregateError 实例化时使用的迭代器;例如,如果 AggregateError 是用 AggregateError() 构造函数创建的,则作为第一个参数传递给构造函数的任何迭代器生成的数组。

实例方法

从父类 Error 中继承实例方法。

示例

捕获一个 AggregateError

js
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

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" ]
}

规范

Specification
ECMAScript® 2025 Language Specification
# sec-aggregate-error-objects

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
AggregateError
AggregateError() constructor
errors
AggregateError is serializable

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support
See implementation notes.

参见