From 49745cdef08e43d87123058798497bba49879c93 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Tue, 17 Mar 2020 02:17:10 +0100 Subject: [PATCH] process: delay throwing an error using `throwDeprecation` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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: https://github.com/nodejs/node/issues/17871 PR-URL: https://github.com/nodejs/node/pull/32312 Reviewed-By: Benjamin Gruenbaum Reviewed-By: Matteo Collina Reviewed-By: Michaƫl Zasso --- lib/internal/process/warning.js | 9 +++++++-- test/parallel/test-process-warning.js | 11 ++++++++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/internal/process/warning.js b/lib/internal/process/warning.js index ebf4c932fa9c57..82bff2ece9e146 100644 --- a/lib/internal/process/warning.js +++ b/lib/internal/process/warning.js @@ -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); } diff --git a/test/parallel/test-process-warning.js b/test/parallel/test-process-warning.js index e4538ddc084722..c1fbbf775fb45e 100644 --- a/test/parallel/test-process-warning.js +++ b/test/parallel/test-process-warning.js @@ -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); }