1
0
mirror of https://github.com/ruanyf/es6tutorial.git synced 2025-05-24 18:32:22 +00:00

docs(object): add AggregateError

This commit is contained in:
ruanyf 2021-09-19 15:54:44 +08:00
parent a687c3927d
commit 1fdd34ebbf

View File

@ -704,3 +704,52 @@ let aWithXGetter = { ...a }; // 报错
上面例子中,取值函数`get`在扩展`a`对象时会自动执行,导致报错。
## AggregateError 错误对象
ES2021 标准之中,伴随`Promise.any()`方法,还引入一个新的错误对象`AggregateError`,也放在这一章介绍。
AggregateError 在一个错误对象里面,封装了多个错误。如果某个单一操作,同时引发了多个错误,,需要同时抛出这些错误,那么就可以抛出一个 AggregateError 错误对象,把各种错误都放在这个对象里面。
AggregateError 本身是一个构造函数,用来生成 AggregateError 实例对象。
```javascript
AggregateError(errors[, message])
```
`AggregateError()`构造函数可以接受两个参数。
- errors数组它的每个成员都是一个错误对象。该参数是必须的。
- message字符串表示 AggregateError 抛出时的提示信息。该参数是可选的。
```javascript
const error = new AggregateError([
new Error('ERROR_11112'),
new TypeError('First name must be a string'),
new RangeError('Transaction value must be at least 1'),
new URIError('User profile link must be https'),
], 'Transaction cannot be processed')
```
上面示例中,`AggregateError()`的第一个参数数组里面,一共有四个错误实例。第二个参数字符串则是这四个错误的一个整体的提示。
`AggregateError`的实例对象有三个属性。
- name错误名称默认为“AggregateError”。
- message错误的提示信息。
- errors数组每个成员都是一个错误对象。
下面是一个示例。
```javascript
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" ]
}
```