GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — module: mark evaluation rejection in require(esm) as handled · nodejs/node@3d89e6b

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src
    • module_wrap.cc
  • test
    • es-module
      • test-require-module-error-catching.js
      • test-require-module-synchronous-rejection-handling.js
    • fixtures/es-modules
      • reference-error-esm.js
      • runtime-error-esm.js
      • synchronous-rejection-esm.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/module_wrap.cc‎

Copy file name to clipboardExpand all lines: src/module_wrap.cc
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,22 @@ void ModuleWrap::EvaluateSync(const FunctionCallbackInfo<Value>& args) {
609609
CHECK(result->IsPromise());
610610
Local<Promise> promise = result.As<Promise>();
611611
if (promise->State() == Promise::PromiseState::kRejected) {
612+
// The rejected promise is created by V8, so we don't get a chance to mark
613+
// it as resolved before the rejection happens from evaluation. But we can
614+
// tell the promise rejection callback to treat it as a promise rejected
615+
// before handler was added which would remove it from the unhandled
616+
// rejection handling, since we are converting it into an error and throw
617+
// from here directly.
618+
Local<Value> type = v8::Integer::New(
619+
isolate,
620+
static_cast<int32_t>(
621+
v8::PromiseRejectEvent::kPromiseHandlerAddedAfterReject));
622+
Local<Value> args[] = {type, promise, Undefined(isolate)};
623+
if (env->promise_reject_callback()
624+
->Call(context, Undefined(isolate), arraysize(args), args)
625+
.IsEmpty()) {
626+
return;
627+
}
612628
Local<Value> exception = promise->Result();
613629
Local<v8::Message> message =
614630
v8::Exception::CreateMessage(isolate, exception);
Collapse file

‎test/es-module/test-require-module-error-catching.js‎

Copy file name to clipboard
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// This tests synchronous errors in ESM from require(esm) can be caught.
2+
3+
'use strict';
4+
5+
require('../common');
6+
const assert = require('assert');
7+
8+
// Runtime errors from throw should be caught.
9+
assert.throws(() => {
10+
require('../fixtures/es-modules/runtime-error-esm.js');
11+
}, {
12+
message: 'hello'
13+
});
14+
15+
// References errors should be caught too.
16+
assert.throws(() => {
17+
require('../fixtures/es-modules/reference-error-esm.js');
18+
}, {
19+
name: 'ReferenceError',
20+
message: 'exports is not defined'
21+
});
Collapse file

‎test/es-module/test-require-module-synchronous-rejection-handling.js‎

Copy file name to clipboard
+13Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// This synchronous rejections from require(esm) still go to the unhandled rejection
2+
// handler.
3+
4+
'use strict';
5+
6+
const common = require('../common');
7+
const assert = require('assert');
8+
9+
process.on('unhandledRejection', common.mustCall((reason, promise) => {
10+
assert.strictEqual(reason, 'reject!');
11+
}));
12+
13+
require('../fixtures/es-modules/synchronous-rejection-esm.js');
Collapse file

‎test/fixtures/es-modules/reference-error-esm.js‎

Copy file name to clipboard
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// This module is invalid in both ESM and CJS, because
2+
// 'exports' are not defined in ESM, while require cannot be
3+
// redeclared in CJS.
4+
Object.defineProperty(exports, "__esModule", { value: true });
5+
const require = () => {};
Collapse file

‎test/fixtures/es-modules/runtime-error-esm.js‎

Copy file name to clipboard
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import 'node:fs'; // Forces it to be recognized as ESM.
2+
throw new Error('hello');
Collapse file

‎test/fixtures/es-modules/synchronous-rejection-esm.js‎

Copy file name to clipboard
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import 'node:fs'; // Forces it to be recognized as ESM.
2+
Promise.reject('reject!');

0 commit comments

Comments
0 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.