Skip to content

Commit 3cf3fb9

Browse files
aduh95ruyadorno
authored andcommittedSep 12, 2023
doc: revise error.md introduction
PR-URL: #48423 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Mestery <mestery@protonmail.com>
1 parent 7575d8b commit 3cf3fb9

File tree

1 file changed

+28
-61
lines changed

1 file changed

+28
-61
lines changed
 

‎doc/api/errors.md

+28-61
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,35 @@ try {
4646
```
4747

4848
Any use of the JavaScript `throw` mechanism will raise an exception that
49-
_must_ be handled using `try…catch` or the Node.js process will exit
50-
immediately.
49+
_must_ be handled or the Node.js process will exit immediately.
5150

5251
With few exceptions, _Synchronous_ APIs (any blocking method that does not
53-
accept a `callback` function, such as [`fs.readFileSync`][]), will use `throw`
54-
to report errors.
52+
return a {Promise} nor accept a `callback` function, such as
53+
[`fs.readFileSync`][]), will use `throw` to report errors.
5554

5655
Errors that occur within _Asynchronous APIs_ may be reported in multiple ways:
5756

57+
* Some asynchronous methods returns a {Promise}, you should always take into
58+
account that it might be rejected. See [`--unhandled-rejections`][] flag for
59+
how the process will react to an unhandled promise rejection.
60+
61+
<!-- eslint-disable no-useless-return -->
62+
63+
```js
64+
const fs = require('fs/promises');
65+
66+
(async () => {
67+
let data;
68+
try {
69+
data = await fs.readFile('a file that does not exist');
70+
} catch (err) {
71+
console.error('There was an error reading the file!', err);
72+
return;
73+
}
74+
// Otherwise handle the data
75+
})();
76+
```
77+
5878
* Most asynchronous methods that accept a `callback` function will accept an
5979
`Error` object passed as the first argument to that function. If that first
6080
argument is not `null` and is an instance of `Error`, then an error occurred
@@ -104,9 +124,9 @@ pass or fail).
104124

105125
For _all_ [`EventEmitter`][] objects, if an `'error'` event handler is not
106126
provided, the error will be thrown, causing the Node.js process to report an
107-
uncaught exception and crash unless either: The [`domain`][domains] module is
108-
used appropriately or a handler has been registered for the
109-
[`'uncaughtException'`][] event.
127+
uncaught exception and crash unless either: a handler has been registered for
128+
the [`'uncaughtException'`][] event, or the deprecated [`node:domain`][domains]
129+
module is used.
110130

111131
```js
112132
const EventEmitter = require('node:events');
@@ -125,60 +145,6 @@ they are thrown _after_ the calling code has already exited.
125145
Developers must refer to the documentation for each method to determine
126146
exactly how errors raised by those methods are propagated.
127147

128-
### Error-first callbacks
129-
130-
<!--type=misc-->
131-
132-
Most asynchronous methods exposed by the Node.js core API follow an idiomatic
133-
pattern referred to as an _error-first callback_. With this pattern, a callback
134-
function is passed to the method as an argument. When the operation either
135-
completes or an error is raised, the callback function is called with the
136-
`Error` object (if any) passed as the first argument. If no error was raised,
137-
the first argument will be passed as `null`.
138-
139-
```js
140-
const fs = require('node:fs');
141-
142-
function errorFirstCallback(err, data) {
143-
if (err) {
144-
console.error('There was an error', err);
145-
return;
146-
}
147-
console.log(data);
148-
}
149-
150-
fs.readFile('/some/file/that/does-not-exist', errorFirstCallback);
151-
fs.readFile('/some/file/that/does-exist', errorFirstCallback);
152-
```
153-
154-
The JavaScript `try…catch` mechanism **cannot** be used to intercept errors
155-
generated by asynchronous APIs. A common mistake for beginners is to try to
156-
use `throw` inside an error-first callback:
157-
158-
```js
159-
// THIS WILL NOT WORK:
160-
const fs = require('node:fs');
161-
162-
try {
163-
fs.readFile('/some/file/that/does-not-exist', (err, data) => {
164-
// Mistaken assumption: throwing here...
165-
if (err) {
166-
throw err;
167-
}
168-
});
169-
} catch (err) {
170-
// This will not catch the throw!
171-
console.error(err);
172-
}
173-
```
174-
175-
This will not work because the callback function passed to `fs.readFile()` is
176-
called asynchronously. By the time the callback has been called, the
177-
surrounding code, including the `try…catch` block, will have already exited.
178-
Throwing an error inside the callback **can crash the Node.js process** in most
179-
cases. If [domains][] are enabled, or a handler has been registered with
180-
`process.on('uncaughtException')`, such errors can be intercepted.
181-
182148
## Class: `Error`
183149

184150
<!--type=class-->
@@ -3576,6 +3542,7 @@ The native call from `process.cpuUsage` could not be processed.
35763542
[`--disable-proto=throw`]: cli.md#--disable-protomode
35773543
[`--force-fips`]: cli.md#--force-fips
35783544
[`--no-addons`]: cli.md#--no-addons
3545+
[`--unhandled-rejections`]: cli.md#--unhandled-rejectionsmode
35793546
[`Class: assert.AssertionError`]: assert.md#class-assertassertionerror
35803547
[`ERR_INVALID_ARG_TYPE`]: #err_invalid_arg_type
35813548
[`ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST`]: #err_missing_message_port_in_transfer_list

0 commit comments

Comments
 (0)
Please sign in to comment.