From 9dcc9b6a6b3b8ab40be91b2fdc6fdf514e48dcc3 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Mon, 11 Mar 2019 20:46:43 +0100 Subject: [PATCH] process: add --unhandled-rejections flag MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This adds a flag to define the default behavior for unhandled rejections. Three modes exist: `none`, `warn` and `strict`. The first is going to silence all unhandled rejection warnings. The second behaves identical to the current default with the excetion that no deprecation warning will be printed and the last is going to throw an error for each unhandled rejection, just as regular exceptions do. It is possible to intercept those with the `uncaughtException` hook as with all other exceptions as well. This PR has no influence on the existing `unhandledRejection` hook. If that is used, it will continue to function as before. PR-URL: https://github.com/nodejs/node/pull/26599 Reviewed-By: Benjamin Gruenbaum Reviewed-By: Matteo Collina Reviewed-By: Matheus Marchini Reviewed-By: Michael Dawson Reviewed-By: Сковорода Никита Андреевич Reviewed-By: Sakthipriyan Vairamani --- doc/api/cli.md | 19 ++++ doc/api/process.md | 37 +++++--- doc/node.1 | 3 + lib/internal/main/worker_thread.js | 4 +- lib/internal/modules/cjs/loader.js | 5 +- lib/internal/process/execution.js | 5 +- lib/internal/process/promises.js | 92 +++++++++++++++---- lib/internal/process/task_queues.js | 6 +- src/node_errors.cc | 15 ++- src/node_errors.h | 5 + src/node_options.cc | 18 +++- src/node_options.h | 1 + src/node_task_queue.cc | 3 +- .../message/promise_always_throw_unhandled.js | 16 ++++ .../promise_always_throw_unhandled.out | 13 +++ test/parallel/test-cli-node-options.js | 1 + test/parallel/test-next-tick-errors.js | 4 +- test/parallel/test-promise-unhandled-error.js | 54 +++++++++++ test/parallel/test-promise-unhandled-flag.js | 16 ++++ .../test-promise-unhandled-silent-no-hook.js | 22 +++++ .../parallel/test-promise-unhandled-silent.js | 23 +++++ .../test-promise-unhandled-warn-no-hook.js | 23 +++++ test/parallel/test-promise-unhandled-warn.js | 28 ++++++ .../test-timers-immediate-queue-throw.js | 7 +- .../test-timers-unref-throw-then-ref.js | 6 +- 25 files changed, 376 insertions(+), 50 deletions(-) create mode 100644 test/message/promise_always_throw_unhandled.js create mode 100644 test/message/promise_always_throw_unhandled.out create mode 100644 test/parallel/test-promise-unhandled-error.js create mode 100644 test/parallel/test-promise-unhandled-flag.js create mode 100644 test/parallel/test-promise-unhandled-silent-no-hook.js create mode 100644 test/parallel/test-promise-unhandled-silent.js create mode 100644 test/parallel/test-promise-unhandled-warn-no-hook.js create mode 100644 test/parallel/test-promise-unhandled-warn.js diff --git a/doc/api/cli.md b/doc/api/cli.md index 60d75a6c04baef..f56eb981a986e4 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -577,6 +577,23 @@ added: v2.4.0 Track heap object allocations for heap snapshots. +### `--unhandled-rejections=mode` + + +By default all unhandled rejections trigger a warning plus a deprecation warning +for the very first unhandled rejection in case no [`unhandledRejection`][] hook +is used. + +Using this flag allows to change what should happen when an unhandled rejection +occurs. One of three modes can be chosen: + +* `strict`: Raise the unhandled rejection as an uncaught exception. +* `warn`: Always trigger a warning, no matter if the [`unhandledRejection`][] + hook is set or not but do not print the deprecation warning. +* `none`: Silence all warnings. + ### `--use-bundled-ca`, `--use-openssl-ca` +* `err` {Error} The uncaught exception. +* `origin` {string} Indicates if the exception originates from an unhandled + rejection or from synchronous errors. Can either be `'uncaughtException'` or + `'unhandledRejection'`. + The `'uncaughtException'` event is emitted when an uncaught JavaScript exception bubbles all the way back to the event loop. By default, Node.js handles such exceptions by printing the stack trace to `stderr` and exiting @@ -217,12 +226,13 @@ behavior. Alternatively, change the [`process.exitCode`][] in the provided exit code. Otherwise, in the presence of such handler the process will exit with 0. -The listener function is called with the `Error` object passed as the only -argument. - ```js -process.on('uncaughtException', (err) => { - fs.writeSync(1, `Caught exception: ${err}\n`); +process.on('uncaughtException', (err, origin) => { + fs.writeSync( + process.stderr.fd, + `Caught exception: ${err}\n` + + `Exception origin: ${origin}` + ); }); setTimeout(() => { @@ -274,6 +284,10 @@ changes: a process warning. --> +* `reason` {Error|any} The object with which the promise was rejected + (typically an [`Error`][] object). +* `promise` {Promise} The rejected promise. + The `'unhandledRejection'` event is emitted whenever a `Promise` is rejected and no error handler is attached to the promise within a turn of the event loop. When programming with Promises, exceptions are encapsulated as "rejected @@ -282,15 +296,9 @@ are propagated through a `Promise` chain. The `'unhandledRejection'` event is useful for detecting and keeping track of promises that were rejected whose rejections have not yet been handled. -The listener function is called with the following arguments: - -* `reason` {Error|any} The object with which the promise was rejected - (typically an [`Error`][] object). -* `p` the `Promise` that was rejected. - ```js -process.on('unhandledRejection', (reason, p) => { - console.log('Unhandled Rejection at:', p, 'reason:', reason); +process.on('unhandledRejection', (reason, promise) => { + console.log('Unhandled Rejection at:', promise, 'reason:', reason); // Application specific logging, throwing an error, or other logic here }); @@ -317,7 +325,7 @@ as would typically be the case for other `'unhandledRejection'` events. To address such failures, a non-operational [`.catch(() => { })`][`promise.catch()`] handler may be attached to `resource.loaded`, which would prevent the `'unhandledRejection'` event from -being emitted. Alternatively, the [`'rejectionHandled'`][] event may be used. +being emitted. ### Event: 'warning'