Skip to content

Commit 976093e

Browse files
idedanielleadams
authored andcommitted
doc: document ES2022's Error "cause" property
ES2022 adds an `options` parameter to the `Error` constructor. If the options argument contains a property named `cause`, the property's value is assigned to a non-enumerable property named `cause` on the newly created error. The `cause` property is not referenced anywhere else in the ES2022/2023 specifications. It is for error-formatting software like `util.inspect()` to consume. The `cause` property was added in V8 9.3, which was added to Node 16.9.0. Refs: https://tc39.es/ecma262/#sec-error-message Refs: https://github.com/nodejs/node/blob/main/doc/changelogs/CHANGELOG_V16.md#error-cause PR-URL: #43830 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
1 parent aba9c8e commit 976093e

File tree

1 file changed

+49
-2
lines changed

1 file changed

+49
-2
lines changed

doc/api/errors.md

+49-2
Original file line numberDiff line numberDiff line change
@@ -191,13 +191,16 @@ provide a text description of the error.
191191
All errors generated by Node.js, including all system and JavaScript errors,
192192
will either be instances of, or inherit from, the `Error` class.
193193

194-
### `new Error(message)`
194+
### `new Error(message[, options])`
195195

196196
* `message` {string}
197+
* `options` {Object}
198+
* `cause` {any} The error that caused the newly created error.
197199

198200
Creates a new `Error` object and sets the `error.message` property to the
199201
provided text message. If an object is passed as `message`, the text message
200-
is generated by calling `message.toString()`. The `error.stack` property will
202+
is generated by calling `String(message)`. If the `cause` option is provided,
203+
it is assigned to the `error.cause` property. The `error.stack` property will
201204
represent the point in the code at which `new Error()` was called. Stack traces
202205
are dependent on [V8's stack trace API][]. Stack traces extend only to either
203206
(a) the beginning of _synchronous code execution_, or (b) the number of frames
@@ -253,6 +256,49 @@ will affect any stack trace captured _after_ the value has been changed.
253256
If set to a non-number value, or set to a negative number, stack traces will
254257
not capture any frames.
255258

259+
### `error.cause`
260+
261+
<!-- YAML
262+
added: v16.9.0
263+
-->
264+
265+
* {any}
266+
267+
If present, the `error.cause` property is the underlying cause of the `Error`.
268+
It is used when catching an error and throwing a new one with a different
269+
message or code in order to still have access to the original error.
270+
271+
The `error.cause` property is typically set by calling
272+
`new Error(message, { cause })`. It is not set by the constructor if the
273+
`cause` option is not provided.
274+
275+
This property allows errors to be chained. When serializing `Error` objects,
276+
[`util.inspect()`][] recursively serializes `error.cause` if it is set.
277+
278+
```js
279+
const cause = new Error('The remote HTTP server responded with a 500 status');
280+
const symptom = new Error('The message failed to send', { cause });
281+
282+
console.log(symptom);
283+
// Prints:
284+
// Error: The message failed to send
285+
// at REPL2:1:17
286+
// at Script.runInThisContext (node:vm:130:12)
287+
// ... 7 lines matching cause stack trace ...
288+
// at [_line] [as _line] (node:internal/readline/interface:886:18) {
289+
// [cause]: Error: The remote HTTP server responded with a 500 status
290+
// at REPL1:1:15
291+
// at Script.runInThisContext (node:vm:130:12)
292+
// at REPLServer.defaultEval (node:repl:574:29)
293+
// at bound (node:domain:426:15)
294+
// at REPLServer.runBound [as eval] (node:domain:437:12)
295+
// at REPLServer.onLine (node:repl:902:10)
296+
// at REPLServer.emit (node:events:549:35)
297+
// at REPLServer.emit (node:domain:482:12)
298+
// at [_onLine] [as _onLine] (node:internal/readline/interface:425:12)
299+
// at [_line] [as _line] (node:internal/readline/interface:886:18)
300+
```
301+
256302
### `error.code`
257303

258304
* {string}
@@ -3514,6 +3560,7 @@ The native call from `process.cpuUsage` could not be processed.
35143560
[`subprocess.send()`]: child_process.md#subprocesssendmessage-sendhandle-options-callback
35153561
[`url.parse()`]: url.md#urlparseurlstring-parsequerystring-slashesdenotehost
35163562
[`util.getSystemErrorName(error.errno)`]: util.md#utilgetsystemerrornameerr
3563+
[`util.inspect()`]: util.md#utilinspectobject-options
35173564
[`util.parseArgs()`]: util.md#utilparseargsconfig
35183565
[`v8.startupSnapshot.setDeserializeMainFunction()`]: v8.md#v8startupsnapshotsetdeserializemainfunctioncallback-data
35193566
[`zlib`]: zlib.md

0 commit comments

Comments
 (0)