Skip to content

Commit b6a7052

Browse files
joyeecheungtargos
authored andcommitted
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: #28258 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Franziska Hinkelmann <[email protected]>
1 parent 3a2e67b commit b6a7052

File tree

2 files changed

+13
-15
lines changed

2 files changed

+13
-15
lines changed

lib/internal/process/promises.js

+13-14
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ function handledRejection(promise) {
126126
}
127127

128128
const unhandledRejectionErrName = 'UnhandledPromiseRejectionWarning';
129-
function emitWarning(uid, reason) {
129+
function emitUnhandledRejectionWarning(uid, reason) {
130130
const warning = getError(
131131
unhandledRejectionErrName,
132132
'Unhandled promise rejection. This error originated either by ' +
@@ -144,20 +144,15 @@ function emitWarning(uid, reason) {
144144
} catch {}
145145

146146
process.emitWarning(warning);
147-
emitDeprecationWarning();
148147
}
149148

150149
let deprecationWarned = false;
151150
function emitDeprecationWarning() {
152-
if (unhandledRejectionsMode === kDefaultUnhandledRejections &&
153-
!deprecationWarned) {
154-
deprecationWarned = true;
155-
process.emitWarning(
156-
'Unhandled promise rejections are deprecated. In the future, ' +
157-
'promise rejections that are not handled will terminate the ' +
158-
'Node.js process with a non-zero exit code.',
159-
'DeprecationWarning', 'DEP0018');
160-
}
151+
process.emitWarning(
152+
'Unhandled promise rejections are deprecated. In the future, ' +
153+
'promise rejections that are not handled will terminate the ' +
154+
'Node.js process with a non-zero exit code.',
155+
'DeprecationWarning', 'DEP0018');
161156
}
162157

163158
// If this method returns true, we've executed user code or triggered
@@ -186,7 +181,7 @@ function processPromiseRejections() {
186181
case kThrowUnhandledRejections: {
187182
fatalException(reason);
188183
const handled = process.emit('unhandledRejection', reason, promise);
189-
if (!handled) emitWarning(uid, reason);
184+
if (!handled) emitUnhandledRejectionWarning(uid, reason);
190185
break;
191186
}
192187
case kIgnoreUnhandledRejections: {
@@ -195,12 +190,16 @@ function processPromiseRejections() {
195190
}
196191
case kAlwaysWarnUnhandledRejections: {
197192
process.emit('unhandledRejection', reason, promise);
198-
emitWarning(uid, reason);
193+
emitUnhandledRejectionWarning(uid, reason);
199194
break;
200195
}
201196
case kDefaultUnhandledRejections: {
202197
const handled = process.emit('unhandledRejection', reason, promise);
203-
if (!handled) emitWarning(uid, reason);
198+
if (!handled) emitUnhandledRejectionWarning(uid, reason);
199+
if (!deprecationWarned) {
200+
emitDeprecationWarning();
201+
deprecationWarned = true;
202+
}
204203
break;
205204
}
206205
}

test/message/unhandled_promise_trace_warnings.out

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
at *
2222
at *
2323
at *
24-
at *
2524
(node:*) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously (rejection id: 1)
2625
at handledRejection (internal/process/promises.js:*)
2726
at promiseRejectHandler (internal/process/promises.js:*)

0 commit comments

Comments
 (0)