Skip to content

Commit

Permalink
lib: refactor unhandled rejection deprecation warning emission
Browse files Browse the repository at this point in the history
Emit the deprecation warning in the `kDefaultUnhandledRejections`
case to reduce the number of branches on unhandled rejection mode -
there is now only one switch case on it.

Also rename `emitWarning()` to `emitUnhandledRejectionWarning()`
to avoid ambiguity with `process.emitWarning()`

PR-URL: #28258
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Franziska Hinkelmann <[email protected]>
  • Loading branch information
joyeecheung authored and targos committed Jul 2, 2019
1 parent 3a2e67b commit b6a7052
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
27 changes: 13 additions & 14 deletions lib/internal/process/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ function handledRejection(promise) {
}

const unhandledRejectionErrName = 'UnhandledPromiseRejectionWarning';
function emitWarning(uid, reason) {
function emitUnhandledRejectionWarning(uid, reason) {
const warning = getError(
unhandledRejectionErrName,
'Unhandled promise rejection. This error originated either by ' +
Expand All @@ -144,20 +144,15 @@ function emitWarning(uid, reason) {
} catch {}

process.emitWarning(warning);
emitDeprecationWarning();
}

let deprecationWarned = false;
function emitDeprecationWarning() {
if (unhandledRejectionsMode === kDefaultUnhandledRejections &&
!deprecationWarned) {
deprecationWarned = true;
process.emitWarning(
'Unhandled promise rejections are deprecated. In the future, ' +
'promise rejections that are not handled will terminate the ' +
'Node.js process with a non-zero exit code.',
'DeprecationWarning', 'DEP0018');
}
process.emitWarning(
'Unhandled promise rejections are deprecated. In the future, ' +
'promise rejections that are not handled will terminate the ' +
'Node.js process with a non-zero exit code.',
'DeprecationWarning', 'DEP0018');
}

// If this method returns true, we've executed user code or triggered
Expand Down Expand Up @@ -186,7 +181,7 @@ function processPromiseRejections() {
case kThrowUnhandledRejections: {
fatalException(reason);
const handled = process.emit('unhandledRejection', reason, promise);
if (!handled) emitWarning(uid, reason);
if (!handled) emitUnhandledRejectionWarning(uid, reason);
break;
}
case kIgnoreUnhandledRejections: {
Expand All @@ -195,12 +190,16 @@ function processPromiseRejections() {
}
case kAlwaysWarnUnhandledRejections: {
process.emit('unhandledRejection', reason, promise);
emitWarning(uid, reason);
emitUnhandledRejectionWarning(uid, reason);
break;
}
case kDefaultUnhandledRejections: {
const handled = process.emit('unhandledRejection', reason, promise);
if (!handled) emitWarning(uid, reason);
if (!handled) emitUnhandledRejectionWarning(uid, reason);
if (!deprecationWarned) {
emitDeprecationWarning();
deprecationWarned = true;
}
break;
}
}
Expand Down
1 change: 0 additions & 1 deletion test/message/unhandled_promise_trace_warnings.out
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
at *
at *
at *
at *
(node:*) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously (rejection id: 1)
at handledRejection (internal/process/promises.js:*)
at promiseRejectHandler (internal/process/promises.js:*)
Expand Down

0 comments on commit b6a7052

Please sign in to comment.