-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
module: mark evaluation rejection in require(esm) as handled
Previously the implemention of require(esm) only converted the rejected promise from module evaluation into an error, but the rejected promise was still treated as a pending unhandled rejection by the promise rejection callback, because the promise is created by V8 internals and we don't get a chance to mark it as handled, so the rejection incorrectly marked as unhandled would still go through unhandled rejection handling (if no global listener is set, the default handling would print a warning and make the Node.js instance exit with 1). This patch fixes it by calling into the JS promise rejection callback to mark the evalaution rejection handled so that it doesn't go through unhandled rejection handling. PR-URL: #56122 Fixes: #56115 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
- Loading branch information
1 parent
fc76ca0
commit 81f493d
Showing
6 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// This tests synchronous errors in ESM from require(esm) can be caught. | ||
|
||
'use strict'; | ||
|
||
require('../common'); | ||
const assert = require('assert'); | ||
|
||
// Runtime errors from throw should be caught. | ||
assert.throws(() => { | ||
require('../fixtures/es-modules/runtime-error-esm.js'); | ||
}, { | ||
message: 'hello' | ||
}); | ||
|
||
// References errors should be caught too. | ||
assert.throws(() => { | ||
require('../fixtures/es-modules/reference-error-esm.js'); | ||
}, { | ||
name: 'ReferenceError', | ||
message: 'exports is not defined' | ||
}); |
13 changes: 13 additions & 0 deletions
13
test/es-module/test-require-module-synchronous-rejection-handling.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// This synchronous rejections from require(esm) still go to the unhandled rejection | ||
// handler. | ||
|
||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
|
||
process.on('unhandledRejection', common.mustCall((reason, promise) => { | ||
assert.strictEqual(reason, 'reject!'); | ||
})); | ||
|
||
require('../fixtures/es-modules/synchronous-rejection-esm.js'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// This module is invalid in both ESM and CJS, because | ||
// 'exports' are not defined in ESM, while require cannot be | ||
// redeclared in CJS. | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const require = () => {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import 'node:fs'; // Forces it to be recognized as ESM. | ||
throw new Error('hello'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import 'node:fs'; // Forces it to be recognized as ESM. | ||
Promise.reject('reject!'); |