From d5409f41ebf6bd6a957abda5be029cc1de4fea18 Mon Sep 17 00:00:00 2001 From: Dan Fabulich Date: Thu, 23 Apr 2020 00:17:18 -0700 Subject: [PATCH] process: Don't warn/throw on unhandled rejections when hook is set --unhandled-rejections has three explicit modes (strict, warn, none) plus one implicit "default" mode, which logs an additional deprecation warning (DEP0018). Prior to this commit, the default mode was subtly different from warn mode. If the unhandledRejections hook is set, default mode suppresses all warnings. In warn mode, unhandledRejections would always fire a warning, regardless of whether the hook was set. In addition, prior to this commit, strict mode would always throw an exception, regardless of whether the hook was set. In this commit, all modes honor the unhandledRejections hook. If the user has set the hook, then the user has taken full responsibility over the behavior of unhandled rejections. In that case, no additional warnings or thrown exceptions will be fired, even in warn mode or strict mode. This commit is a stepping stone towards resolving DEP0018. After this commit, any code that includes an unhandledRejection hook will behave unchanged when we change the default mode. Refs: https://github.com/nodejs/node/pull/26599 --- doc/api/cli.md | 18 ++++-- lib/internal/process/promises.js | 63 +++++++++---------- .../message/promise_always_throw_unhandled.js | 4 +- test/parallel/test-promise-unhandled-error.js | 1 - test/parallel/test-promise-unhandled-warn.js | 2 +- ...est-promises-unhandled-proxy-rejections.js | 5 +- ...st-promises-unhandled-symbol-rejections.js | 5 +- 7 files changed, 53 insertions(+), 45 deletions(-) diff --git a/doc/api/cli.md b/doc/api/cli.md index b56a77143863d4..076f4aa4495f43 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -890,12 +890,20 @@ 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: +occurs and the [`unhandledRejection`][] hook is unset. -* `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. +One of three modes can be chosen: + +* `strict`: Emit [`unhandledRejection`][]. If this hook is not set, raise the + unhandled rejection as an uncaught exception. +* `warn`: Emit [`unhandledRejection`][]. If this hook is not set, trigger a + warning. This is the default. +* `none`: Emit [`unhandledRejection`][] and do nothing further. + +In all three modes, setting the [`unhandledRejection`][] hook will suppress +the exception/warning. The hook implementation can raise an exception (as in +`strict` mode), log a warning (as in `warn` mode), or do nothing (as in `none` +mode). ### `--use-bundled-ca`, `--use-openssl-ca`