Skip to content

Commit

Permalink
process: unhandledRejection support more errors
Browse files Browse the repository at this point in the history
Support cross realm errors where `instanceof` errors in our
unhandledRejection warning to print better error with stack traces.
  • Loading branch information
benjamingr committed Jan 24, 2022
1 parent ce41395 commit a03aed8
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions lib/internal/process/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const {
ArrayPrototypeShift,
Error,
ObjectDefineProperty,
ObjectHasOwn,
SafeWeakMap,
} = primordials;

Expand Down Expand Up @@ -179,14 +180,21 @@ function emitUnhandledRejectionWarning(uid, reason) {
`(rejection id: ${uid})`
);
try {
if (reason instanceof Error) {
if (ObjectHasOwn(reason, 'stack')) {
warning.stack = reason.stack;
process.emitWarning(reason.stack, unhandledRejectionErrName);
} else {
process.emitWarning(
noSideEffectsToString(reason), unhandledRejectionErrName);
}
} catch {}
} catch {
try {
process.emitWarning(
noSideEffectsToString(reason), unhandledRejectionErrName);
} catch {
// Ignore.
}
}

process.emitWarning(warning);
}
Expand Down Expand Up @@ -232,7 +240,7 @@ function processPromiseRejections() {
try {
switch (unhandledRejectionsMode) {
case kStrictUnhandledRejections: {
const err = reason instanceof Error ?
const err = ObjectHasOwn(reason, 'stack') ?
reason : generateUnhandledRejectionError(reason);
// This destroys the async stack, don't clear it after
triggerUncaughtException(err, true /* fromPromise */);
Expand All @@ -259,7 +267,7 @@ function processPromiseRejections() {
case kThrowUnhandledRejections: {
const handled = emit(reason, promise, promiseInfo);
if (!handled) {
const err = reason instanceof Error ?
const err = ObjectHasOwn(reason, 'stack') ?
reason : generateUnhandledRejectionError(reason);
// This destroys the async stack, don't clear it after
triggerUncaughtException(err, true /* fromPromise */);
Expand Down

0 comments on commit a03aed8

Please sign in to comment.