Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

module: mark evaluation rejection in require(esm) as handled #56122

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/module_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,22 @@ void ModuleWrap::EvaluateSync(const FunctionCallbackInfo<Value>& args) {
CHECK(result->IsPromise());
Local<Promise> promise = result.As<Promise>();
if (promise->State() == Promise::PromiseState::kRejected) {
// The rejected promise is created by V8, so we don't get a chance to mark
// it as resolved before the rejection happens from evaluation. But we can
// tell the promise rejection callback to treat it as a promise rejected
// before handler was added which would remove it from the unhandled
// rejection handling, since we are converting it into an error and throw
// from here directly.
Local<Value> type = v8::Integer::New(
isolate,
static_cast<int32_t>(
v8::PromiseRejectEvent::kPromiseHandlerAddedAfterReject));
Local<Value> args[] = {type, promise, Undefined(isolate)};
if (env->promise_reject_callback()
->Call(context, Undefined(isolate), arraysize(args), args)
.IsEmpty()) {
return;
}
Local<Value> exception = promise->Result();
Local<v8::Message> message =
v8::Exception::CreateMessage(isolate, exception);
Expand Down
21 changes: 21 additions & 0 deletions test/es-module/test-require-module-error-catching.js
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'
});
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');
5 changes: 5 additions & 0 deletions test/fixtures/es-modules/reference-error-esm.js
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 = () => {};
2 changes: 2 additions & 0 deletions test/fixtures/es-modules/runtime-error-esm.js
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');
2 changes: 2 additions & 0 deletions test/fixtures/es-modules/synchronous-rejection-esm.js
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!');
Loading