From c5a979ad65a40465879aac33e8fed62a90b6e431 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Mon, 6 Dec 2021 15:25:42 +0100 Subject: [PATCH] util: serialize falsy cause values while inspecting errors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ruben Bridgewater PR-URL: https://github.com/nodejs/node/pull/41097 Fixes: https://github.com/nodejs/node/issues/41096 Reviewed-By: Michaël Zasso Reviewed-By: James M Snell Reviewed-By: Tobias Nießen Reviewed-By: Anto Aravinth --- lib/internal/util/inspect.js | 3 ++- test/parallel/test-util-inspect.js | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index 4457f56a8d8215..659fd7b671e742 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -1282,7 +1282,8 @@ function formatError(err, constructor, tag, ctx, keys) { removeDuplicateErrorKeys(ctx, keys, err, stack); - if (err.cause && (keys.length === 0 || !keys.includes('cause'))) { + if (err.cause !== undefined && + (keys.length === 0 || !keys.includes('cause'))) { keys.push('cause'); } diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js index 3ffaf656487395..07461224847d47 100644 --- a/test/parallel/test-util-inspect.js +++ b/test/parallel/test-util-inspect.js @@ -660,6 +660,16 @@ assert.strictEqual(util.inspect(-5e-324), '-5e-324'); assert(ex.includes('[message]')); } +{ + const falsyCause1 = new Error('', { cause: false }); + delete falsyCause1.stack; + const falsyCause2 = new Error(undefined, { cause: null }); + falsyCause2.stack = ''; + + assert.strictEqual(util.inspect(falsyCause1), '[Error] { [cause]: false }'); + assert.strictEqual(util.inspect(falsyCause2), '[Error] { [cause]: null }'); +} + { const tmp = Error.stackTraceLimit; Error.stackTraceLimit = 0;