Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: fix inspecting error with a throwing getter for cause #47163

Merged
merged 1 commit into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
6 changes: 6 additions & 0 deletions test/message/util-inspect-error-cause.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
10 changes: 10 additions & 0 deletions test/message/util-inspect-error-cause.out
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down