| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -46,15 +46,35 @@ try { | |||
| 46 | 46 | ``` | |
| 47 | 47 | ||
| 48 | 48 | 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. | ||
| 51 | 50 | ||
| 52 | 51 | 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. | ||
| 55 | 54 | ||
| 56 | 55 | Errors that occur within _Asynchronous APIs_ may be reported in multiple ways: | |
| 57 | 56 | ||
| 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 | + | ||
| 58 | 78 | * Most asynchronous methods that accept a `callback` function will accept an | |
| 59 | 79 | `Error` object passed as the first argument to that function. If that first | |
| 60 | 80 | argument is not `null` and is an instance of `Error`, then an error occurred | |
@@ -104,9 +124,9 @@ pass or fail). | |||
| 104 | 124 | ||
| 105 | 125 | For _all_ [`EventEmitter`][] objects, if an `'error'` event handler is not | |
| 106 | 126 | 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. | ||
| 110 | 130 | ||
| 111 | 131 | ```js | |
| 112 | 132 | const EventEmitter = require('node:events'); | |
@@ -125,60 +145,6 @@ they are thrown _after_ the calling code has already exited. | |||
| 125 | 145 | Developers must refer to the documentation for each method to determine | |
| 126 | 146 | exactly how errors raised by those methods are propagated. | |
| 127 | 147 | ||
| 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 | - | ||
| 182 | 148 | ## Class: `Error` | |
| 183 | 149 | ||
| 184 | 150 | <!--type=class--> | |
@@ -3576,6 +3542,7 @@ The native call from `process.cpuUsage` could not be processed. | |||
| 3576 | 3542 | [`--disable-proto=throw`]: cli.md#--disable-protomode | |
| 3577 | 3543 | [`--force-fips`]: cli.md#--force-fips | |
| 3578 | 3544 | [`--no-addons`]: cli.md#--no-addons | |
| 3545 | + [`--unhandled-rejections`]: cli.md#--unhandled-rejectionsmode | ||
| 3579 | 3546 | [`Class: assert.AssertionError`]: assert.md#class-assertassertionerror | |
| 3580 | 3547 | [`ERR_INVALID_ARG_TYPE`]: #err_invalid_arg_type | |
| 3581 | 3548 | [`ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST`]: #err_missing_message_port_in_transfer_list | |
| Back | FazBrowse Home | New Git URL |
0 commit comments