From ab5e94c8325baf66ba6475427ee075ac9668cdb4 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Tue, 21 Dec 2021 15:46:36 -0800 Subject: [PATCH] util: display a present-but-undefined error cause MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See https://github.com/nodejs/node/pull/41097#issuecomment-997055761 PR-URL: https://github.com/nodejs/node/pull/41247 Reviewed-By: Antoine du Hamel Reviewed-By: Colin Ihrig Reviewed-By: Michaël Zasso Reviewed-By: Ruben Bridgewater Reviewed-By: Luigi Pinca Reviewed-By: Tobias Nießen Reviewed-By: James M Snell --- lib/internal/util/inspect.js | 2 +- test/message/util-inspect-error-cause.js | 4 ++++ test/message/util-inspect-error-cause.out | 10 ++++++++++ test/parallel/test-util-inspect.js | 6 ++++++ 4 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index 659fd7b671e742..563ab00262a356 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -1282,7 +1282,7 @@ function formatError(err, constructor, tag, ctx, keys) { removeDuplicateErrorKeys(ctx, keys, err, stack); - if (err.cause !== undefined && + if ('cause' in err && (keys.length === 0 || !keys.includes('cause'))) { keys.push('cause'); } diff --git a/test/message/util-inspect-error-cause.js b/test/message/util-inspect-error-cause.js index 4f22a34056cf4d..23c2b20d8633b2 100644 --- a/test/message/util-inspect-error-cause.js +++ b/test/message/util-inspect-error-cause.js @@ -22,9 +22,13 @@ const cause5 = new Error('Object cause', { ' at Module._compile (node:internal/modules/cjs/loader:827:30)' } }); +const cause6 = new Error('undefined cause', { + cause: undefined +}); console.log(cause4); console.log(cause5); +console.log(cause6); process.nextTick(() => { const error = new RangeError('New Stack Frames', { cause: cause2 }); diff --git a/test/message/util-inspect-error-cause.out b/test/message/util-inspect-error-cause.out index a34ca5b46c8d47..c7a04f4a09309c 100644 --- a/test/message/util-inspect-error-cause.out +++ b/test/message/util-inspect-error-cause.out @@ -23,6 +23,16 @@ Error: Object cause ' at Module._compile (node:internal/modules/cjs/loader:827:30)' } } +Error: undefined cause + at * + at * + at * + at * + at * + at * + at * { + [cause]: undefined +} RangeError: New Stack Frames at * *[90m at *[39m { diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js index 07461224847d47..65736cd62a8111 100644 --- a/test/parallel/test-util-inspect.js +++ b/test/parallel/test-util-inspect.js @@ -665,9 +665,15 @@ assert.strictEqual(util.inspect(-5e-324), '-5e-324'); delete falsyCause1.stack; const falsyCause2 = new Error(undefined, { cause: null }); falsyCause2.stack = ''; + const undefinedCause = new Error('', { cause: undefined }); + undefinedCause.stack = ''; assert.strictEqual(util.inspect(falsyCause1), '[Error] { [cause]: false }'); assert.strictEqual(util.inspect(falsyCause2), '[Error] { [cause]: null }'); + assert.strictEqual( + util.inspect(undefinedCause), + '[Error] { [cause]: undefined }' + ); } {