From 3f03997719df098099f799071337c55fb8d8d0df 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 Signed-off-by: Ruben Bridgewater --- lib/internal/util/inspect.js | 2 +- test/parallel/test-util-inspect.js | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index fb4708bc136957..c94a1d9267a304 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -1265,7 +1265,7 @@ 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 ca9d029e2f62b0..adeaa01c9b69af 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;