diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index e6db302e9be..57ffa96c97d 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -1243,9 +1243,16 @@ function getStackString(error) { function getStackFrames(ctx, err, stack) { const frames = StringPrototypeSplit(stack, '\n'); + let cause; + try { + ({ cause } = err); + } catch { + // If 'cause' is a getter that throws, ignore it. + } + // Remove stack frames identical to frames in cause. - if (err.cause && isError(err.cause)) { - const causeStack = getStackString(err.cause); + if (cause != null && isError(cause)) { + const causeStack = getStackString(cause); const causeStackStart = StringPrototypeIndexOf(causeStack, '\n at'); if (causeStackStart !== -1) { const causeFrames = StringPrototypeSplit(StringPrototypeSlice(causeStack, causeStackStart + 1), '\n'); diff --git a/test/message/util-inspect-error-cause.js b/test/message/util-inspect-error-cause.js index d34a908d374..ed9d8230fe0 100644 --- a/test/message/util-inspect-error-cause.js +++ b/test/message/util-inspect-error-cause.js @@ -46,3 +46,9 @@ process.nextTick(() => { console.log(inspect(cause3)); console.log(inspect(error2)); }); + +{ + const error = new Error('cause that throws'); + Reflect.defineProperty(error, 'cause', { get() { throw new Error(); } }); + console.log(inspect(error)); +} diff --git a/test/message/util-inspect-error-cause.out b/test/message/util-inspect-error-cause.out index c7a04f4a093..73f0a673d76 100644 --- a/test/message/util-inspect-error-cause.out +++ b/test/message/util-inspect-error-cause.out @@ -33,6 +33,16 @@ Error: undefined cause at * { [cause]: undefined } +Error: cause that throws + at * + at * + at * + at * + at * + at * + at * { + [cause]: [Getter] +} RangeError: New Stack Frames at * *[90m at *[39m {