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

lib: refactor unhandled rejection deprecation warning emission #28258

Closed
wants to merge 2 commits into from
Closed
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
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 &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this emit the warning every time rather than just once?

Also - doesn't this break the flag?

cc @BridgeAR

Copy link
Member Author

@joyeecheung joyeecheung Jun 18, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only call site of emitDeprecationWarning() is now L200 which is already guarded with deprecationWarned .

Also the only branch on unhandledRejectionsMode is on L180 so you can see what actions are done for each mode by simply looking at that switch instead of jumping in multiple nested functions.

!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;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The second if should be part of the first one. Otherwise the deprecation notice will be visible even though the user handled the rejection.

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