aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/aggregateerror/index.html
blob: ddc048665321865d8019d16a18e89750cd556ab7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
---
title: AggregateError
slug: Web/JavaScript/Reference/Global_Objects/AggregateError
translation_of: Web/JavaScript/Reference/Global_Objects/AggregateError
---
<blockquote>
<p>{{JSRef}}{{Draft}}{{SeeCompatTable}}</p>
</blockquote>

<p><code><strong>AggregateError</strong></code><font><font>当多个错误​​需要包装在一个错误中时,</font><font></font><font>对象表示一个错误。</font></font></p>

<h2 id="语法">语法</h2>

<pre class="syntaxbox notranslate">new AggregateError(errors[, message])</pre>

<h3 id="参数">参数</h3>

<dl>
 <dt><code>errors</code></dt>
 <dd>错误的描述,默认为空。</dd>
 <dt><code>message</code>{{Optional_Inline}}</dt>
 <dd>AggregateError错误的提示信息。</dd>
</dl>

<h2 id="描述"><font><font>描述</font></font></h2>

<p><font><font>一个</font></font><code>AggregateError</code><font><font>当需要由操作报告多个错误被抛出,例如通过</font></font><a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Promise/any"><code>Promise.any()</code></a><font><font>,在传递给它的所有</font></font><a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Promise/any"><code>Promise</code></a><font><font>拒绝。</font></font></p>

<h2 id="属性">属性</h2>

<dl>
 <dt><code>AggregateError.prototype</code></dt>
 <dd><code>AggregateError</code><font><font>的原</font></font><font><font></font></font></dd>
</dl>

<h2 id="AggregateError_实例">AggregateError 实例</h2>

<h3 id="实例属性">实例属性</h3>

<dl>
 <dt><code>AggregateError.prototype.constructor</code></dt>
 <dd>指定创建实例原型的函数。</dd>
 <dt>{{JSxRef("Error.prototype.message", "AggregateError.prototype.message")}}</dt>
 <dd><font><font>错误消息,默认为</font></font><code>""</code><font><font></font></font></dd>
 <dt>{{JSxRef("Error.prototype.name", "AggregateError.prototype.name")}}</dt>
 <dd><font><font>错误名称,默认为</font></font><code>"AggregateError"</code><font><font></font></font></dd>
</dl>

<h2 id="示例">示例</h2>

<h3 id="捕获一个AggregateError"><font face="x-locale-heading-primary, zillaslab, Palatino, Palatino Linotype, x-locale-heading-secondary, serif"><span style="background-color: #333333;">捕获一个</span></font>AggregateError</h3>

<pre class="brush: js; notranslate">Promise.any([
  Promise.reject(new Error("some error")),
]).catch(e =&gt; {
  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" ]
});
</pre>

<h3 id="创建一个AggregateError">创建一个AggregateError</h3>

<pre class="brush: js; notranslate">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" ]
}
</pre>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td><a href="https://tc39.es/proposal-promise-any/#sec-aggregate-error-constructor">ESNext Promise.any Proposal</a></td>
   <td><span class="spec-Draft">Stage 3 Draft</span></td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>

<div>


<p>{{Compat("javascript.builtins.AggregateError")}}</p>
</div>

<h2 id="相关链接">相关链接</h2>

<ul>
 <li>{{JSxRef("Error")}}</li>
</ul>