From fc00dc04905717454e590161d763dd6a7f3bf0e5 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 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 PR-URL: https://github.com/nodejs/node/pull/41097 Reviewed-By: Michaël Zasso Reviewed-By: James M Snell Reviewed-By: Tobias Nießen Reviewed-By: Anto Aravinth --- 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 367675b92f31f2..4457f56a8d8215 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -1215,7 +1215,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 {