lib: refactor unhandled rejection deprecation warning emission

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: https://github.com/nodejs/node/pull/28258
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
This commit is contained in:
Joyee Cheung 2019-06-17 10:48:21 +08:00
parent 82fe33f18a
commit 1c23b6f2be
No known key found for this signature in database
GPG Key ID: 92B78A53C8303B8D
2 changed files with 13 additions and 15 deletions

View File

@ -126,7 +126,7 @@ function handledRejection(promise) {
} }
const unhandledRejectionErrName = 'UnhandledPromiseRejectionWarning'; const unhandledRejectionErrName = 'UnhandledPromiseRejectionWarning';
function emitWarning(uid, reason) { function emitUnhandledRejectionWarning(uid, reason) {
const warning = getError( const warning = getError(
unhandledRejectionErrName, unhandledRejectionErrName,
'Unhandled promise rejection. This error originated either by ' + 'Unhandled promise rejection. This error originated either by ' +
@ -144,20 +144,15 @@ function emitWarning(uid, reason) {
} catch {} } catch {}
process.emitWarning(warning); process.emitWarning(warning);
emitDeprecationWarning();
} }
let deprecationWarned = false; let deprecationWarned = false;
function emitDeprecationWarning() { function emitDeprecationWarning() {
if (unhandledRejectionsMode === kDefaultUnhandledRejections && process.emitWarning(
!deprecationWarned) { 'Unhandled promise rejections are deprecated. In the future, ' +
deprecationWarned = true; 'promise rejections that are not handled will terminate the ' +
process.emitWarning( 'Node.js process with a non-zero exit code.',
'Unhandled promise rejections are deprecated. In the future, ' + 'DeprecationWarning', 'DEP0018');
'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 // If this method returns true, we've executed user code or triggered
@ -186,7 +181,7 @@ function processPromiseRejections() {
case kThrowUnhandledRejections: { case kThrowUnhandledRejections: {
fatalException(reason); fatalException(reason);
const handled = process.emit('unhandledRejection', reason, promise); const handled = process.emit('unhandledRejection', reason, promise);
if (!handled) emitWarning(uid, reason); if (!handled) emitUnhandledRejectionWarning(uid, reason);
break; break;
} }
case kIgnoreUnhandledRejections: { case kIgnoreUnhandledRejections: {
@ -195,12 +190,16 @@ function processPromiseRejections() {
} }
case kAlwaysWarnUnhandledRejections: { case kAlwaysWarnUnhandledRejections: {
process.emit('unhandledRejection', reason, promise); process.emit('unhandledRejection', reason, promise);
emitWarning(uid, reason); emitUnhandledRejectionWarning(uid, reason);
break; break;
} }
case kDefaultUnhandledRejections: { case kDefaultUnhandledRejections: {
const handled = process.emit('unhandledRejection', reason, promise); const handled = process.emit('unhandledRejection', reason, promise);
if (!handled) emitWarning(uid, reason); if (!handled) emitUnhandledRejectionWarning(uid, reason);
if (!deprecationWarned) {
emitDeprecationWarning();
deprecationWarned = true;
}
break; break;
} }
} }

View File

@ -21,7 +21,6 @@
at * at *
at * at *
at * at *
at *
(node:*) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously (rejection id: 1) (node:*) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously (rejection id: 1)
at handledRejection (internal/process/promises.js:*) at handledRejection (internal/process/promises.js:*)
at promiseRejectHandler (internal/process/promises.js:*) at promiseRejectHandler (internal/process/promises.js:*)