From df5db768f8c71ab4c533f558c6ad81ab556320a8 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Mon, 27 Dec 2021 16:05:35 +0100 Subject: [PATCH 1/6] doc: clarify `uncaughtException` `origin` for ESM Fixes: https://github.com/nodejs/node/issues/41328 --- doc/api/process.md | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/doc/api/process.md b/doc/api/process.md index 1af45fe0240830..5558842729a221 100644 --- a/doc/api/process.md +++ b/doc/api/process.md @@ -337,7 +337,7 @@ changes: rejection or from an synchronous error. Can either be `'uncaughtException'` or `'unhandledRejection'`. The latter is only used in conjunction with the [`--unhandled-rejections`][] flag set to `strict` or `throw` and - an unhandled rejection. + an unhandled rejection, or when the entry point is an ES module. The `'uncaughtException'` event is emitted when an uncaught JavaScript exception bubbles all the way back to the event loop. By default, Node.js @@ -365,6 +365,8 @@ setTimeout(() => { }, 500); // Intentionally cause an exception, but don't catch it. +// Because the exception happens when evaluating an ES module, this is +// undistinguishable from a Promise rejection, and will be reported as such. nonexistentFunc(); console.log('This will not run.'); ``` @@ -433,7 +435,7 @@ added: rejection or from synchronous errors. Can either be `'uncaughtException'` or `'unhandledRejection'`. The latter is only used in conjunction with the [`--unhandled-rejections`][] flag set to `strict` or `throw` and - an unhandled rejection. + an unhandled rejection, or when the entry point is an ES module. The `'uncaughtExceptionMonitor'` event is emitted before an `'uncaughtException'` event is emitted or a hook installed via @@ -451,6 +453,8 @@ process.on('uncaughtExceptionMonitor', (err, origin) => { }); // Intentionally cause an exception, but don't catch it. +// Because the exception happens when evaluating an ES module, this is +// undistinguishable from a Promise rejection, and will be reported as such. nonexistentFunc(); // Still crashes Node.js ``` @@ -503,7 +507,7 @@ process.on('unhandledRejection', (reason, promise) => { somePromise.then((res) => { return reportToUser(JSON.pasre(res)); // Note the typo (`pasre`) -}); // No `.catch()` or `.then()` +}); // No `.catch()` or `.then()` or `await`. ``` ```cjs @@ -531,7 +535,7 @@ function SomeResource() { } const resource = new SomeResource(); -// no .catch or .then on resource.loaded for at least a turn +// no await, .catch, or .then on resource.loaded for at least a turn ``` ```cjs From c3b26924c7287f95f769a70f6884f9b260733ffa Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Mon, 27 Dec 2021 18:20:35 +0100 Subject: [PATCH 2/6] fixup! doc: clarify `uncaughtException` `origin` for ESM --- doc/api/cli.md | 3 +++ doc/api/process.md | 8 +++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/doc/api/cli.md b/doc/api/cli.md index 70a22ad50387fb..43c6103ef923ec 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -1262,6 +1262,9 @@ occurs. One of the following modes can be chosen: set, trigger a warning, and set the process exit code to 1. * `none`: Silence all warnings. +If a rejection happen in the ES module static loading phase, it will always raise +it as an uncaught exception. + ### `--use-bundled-ca`, `--use-openssl-ca`