Skip to content

Commit 2353a5a

Browse files
bartlomiejuclaude
andauthored
fix(ext/node): wrap non-Error unhandled rejections in ERR_UNHANDLED_REJECTION (#32535)
## Summary - When a promise is rejected with a non-Error value (e.g. `Promise.reject(null)`) and there are no `unhandledRejection` listeners, Node.js wraps the rejection reason in an `ERR_UNHANDLED_REJECTION` error before routing it to `uncaughtException`. Deno was passing the raw value directly, which caused crashes when exception handlers accessed `.message` or `.name`. - This fixes `test-promise-unhandled-default.js` in the node compat test suite (also unblocks `test-promise-unhandled-throw.js` which tests the same wrapping logic under `--unhandled-rejections=throw`). --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent e8a3cfa commit 2353a5a

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

ext/node/polyfills/process.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1086,7 +1086,29 @@ function synchronizeListeners() {
10861086
// listeners.
10871087

10881088
event.preventDefault();
1089-
uncaughtExceptionHandler(event.reason, "unhandledRejection");
1089+
1090+
let reason = event.reason;
1091+
// If the rejection reason is not an Error, wrap it in an
1092+
// ERR_UNHANDLED_REJECTION error, matching Node.js behavior.
1093+
if (!(reason instanceof Error)) {
1094+
const message = "This error originated either by throwing " +
1095+
"inside of an async function without a catch block, or by rejecting a " +
1096+
"promise which was not handled with .catch(). The promise rejected with the" +
1097+
` reason "${reason}".`;
1098+
const err = new Error(message);
1099+
// deno-lint-ignore no-explicit-any
1100+
(err as any).code = "ERR_UNHANDLED_REJECTION";
1101+
// deno-lint-ignore no-explicit-any
1102+
(err as any).reason = event.reason;
1103+
Object.defineProperty(err, "name", {
1104+
value: "UnhandledPromiseRejection",
1105+
writable: true,
1106+
configurable: true,
1107+
});
1108+
reason = err;
1109+
}
1110+
1111+
uncaughtExceptionHandler(reason, "unhandledRejection");
10901112
return;
10911113
}
10921114

0 commit comments

Comments
 (0)