Skip to content

Commit

Permalink
process: delay throwing an error using throwDeprecation
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
BridgeAR committed May 23, 2020
1 parent 5d06a37 commit 49745cd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
9 changes: 7 additions & 2 deletions lib/internal/process/warning.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,13 @@ function emitWarning(warning, type, code, ctor) {
if (warning.name === 'DeprecationWarning') {
if (process.noDeprecation)
return;
if (process.throwDeprecation)
throw warning;
if (process.throwDeprecation) {
// Delay throwing the error to guarantee that all former warnings were
// properly logged.
return process.nextTick(() => {
throw warning;
});
}
}
process.nextTick(doEmitWarning, warning);
}
Expand Down
11 changes: 8 additions & 3 deletions test/parallel/test-process-warning.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,14 @@ function test4() {
// process.emitWarning will throw when process.throwDeprecation is true
// and type is `DeprecationWarning`.
process.throwDeprecation = true;
assert.throws(
() => process.emitWarning('test', 'DeprecationWarning'),
/^DeprecationWarning: test$/);
process.once('uncaughtException', (err) => {
assert.match(err.toString(), /^DeprecationWarning: test$/);
});
try {
process.emitWarning('test', 'DeprecationWarning');
} catch {
assert.fail('Unreachable');
}
process.throwDeprecation = false;
setImmediate(test5);
}
Expand Down

0 comments on commit 49745cd

Please sign in to comment.