Skip to content

Commit 49745cd

Browse files
committed
process: delay throwing an error using throwDeprecation
This makes sure all warnings that were triggered before a deprecation warning during the same tick are properly handled and logged. It also guarantees that users can not catch the error anymore. Fixes: #17871 PR-URL: #32312 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
1 parent 5d06a37 commit 49745cd

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

lib/internal/process/warning.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,13 @@ function emitWarning(warning, type, code, ctor) {
126126
if (warning.name === 'DeprecationWarning') {
127127
if (process.noDeprecation)
128128
return;
129-
if (process.throwDeprecation)
130-
throw warning;
129+
if (process.throwDeprecation) {
130+
// Delay throwing the error to guarantee that all former warnings were
131+
// properly logged.
132+
return process.nextTick(() => {
133+
throw warning;
134+
});
135+
}
131136
}
132137
process.nextTick(doEmitWarning, warning);
133138
}

test/parallel/test-process-warning.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,14 @@ function test4() {
3838
// process.emitWarning will throw when process.throwDeprecation is true
3939
// and type is `DeprecationWarning`.
4040
process.throwDeprecation = true;
41-
assert.throws(
42-
() => process.emitWarning('test', 'DeprecationWarning'),
43-
/^DeprecationWarning: test$/);
41+
process.once('uncaughtException', (err) => {
42+
assert.match(err.toString(), /^DeprecationWarning: test$/);
43+
});
44+
try {
45+
process.emitWarning('test', 'DeprecationWarning');
46+
} catch {
47+
assert.fail('Unreachable');
48+
}
4449
process.throwDeprecation = false;
4550
setImmediate(test5);
4651
}

0 commit comments

Comments
 (0)