From cd03389196d4d24f100198281e5316995705e6a6 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Mon, 6 Dec 2021 14:22:30 +0100 Subject: [PATCH] util: make sure error causes of any type may be inspected An error cause may be of any type. Handle all of them, no matter if they are an error or not. Fixes: https://github.com/nodejs/node/issues/41096 Signed-off-by: Ruben Bridgewater --- lib/internal/util/inspect.js | 2 +- test/message/util-inspect-error-cause.js | 13 ++++++++++++ test/message/util-inspect-error-cause.out | 25 +++++++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index 0279e5ecb155f1..fb4708bc136957 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -1198,7 +1198,7 @@ function getStackFrames(ctx, err, stack) { const frames = stack.split('\n'); // Remove stack frames identical to frames in cause. - if (err.cause) { + if (err.cause && isError(err.cause)) { const causeStack = getStackString(err.cause); const causeStackStart = causeStack.indexOf('\n at'); if (causeStackStart !== -1) { diff --git a/test/message/util-inspect-error-cause.js b/test/message/util-inspect-error-cause.js index f6318357974cb6..4f22a34056cf4d 100644 --- a/test/message/util-inspect-error-cause.js +++ b/test/message/util-inspect-error-cause.js @@ -13,6 +13,19 @@ const cause2 = new FoobarError('Individual message', { cause: cause1 }); cause2.extraProperties = 'Yes!'; const cause3 = new Error('Stack causes', { cause: cause2 }); +const cause4 = new Error('Number error cause', { cause: 42 }); +const cause5 = new Error('Object cause', { + cause: { + message: 'Unique', + name: 'Error', + stack: 'Error: Unique\n' + + ' at Module._compile (node:internal/modules/cjs/loader:827:30)' + } +}); + +console.log(cause4); +console.log(cause5); + process.nextTick(() => { const error = new RangeError('New Stack Frames', { cause: cause2 }); const error2 = new RangeError('New Stack Frames', { cause: cause3 }); diff --git a/test/message/util-inspect-error-cause.out b/test/message/util-inspect-error-cause.out index bd8673ae25c031..a34ca5b46c8d47 100644 --- a/test/message/util-inspect-error-cause.out +++ b/test/message/util-inspect-error-cause.out @@ -1,3 +1,28 @@ +Error: Number error cause + at * + at * + at * + at * + at * + at * + at * { + [cause]: 42 +} +Error: Object cause + at * + at * + at * + at * + at * + at * + at * { + [cause]: { + message: 'Unique', + name: 'Error', + stack: 'Error: Unique\n' + + ' at Module._compile (node:internal/modules/cjs/loader:827:30)' + } +} RangeError: New Stack Frames at * *[90m at *[39m {