Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 87ef1b2

Browse filesBrowse files
aduh95danielleadams
authored andcommitted
util: fix inspecting error with a throwing getter for cause
PR-URL: #47163 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
1 parent 3e74a74 commit 87ef1b2
Copy full SHA for 87ef1b2

File tree

Expand file treeCollapse file tree

3 files changed

+25
-2
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+25
-2
lines changed
Open diff view settings
Collapse file

‎lib/internal/util/inspect.js‎

Copy file name to clipboardExpand all lines: lib/internal/util/inspect.js
+9-2Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,9 +1244,16 @@ function getStackString(error) {
12441244
function getStackFrames(ctx, err, stack) {
12451245
const frames = StringPrototypeSplit(stack, '\n');
12461246

1247+
let cause;
1248+
try {
1249+
({ cause } = err);
1250+
} catch {
1251+
// If 'cause' is a getter that throws, ignore it.
1252+
}
1253+
12471254
// Remove stack frames identical to frames in cause.
1248-
if (err.cause && isError(err.cause)) {
1249-
const causeStack = getStackString(err.cause);
1255+
if (cause != null && isError(cause)) {
1256+
const causeStack = getStackString(cause);
12501257
const causeStackStart = StringPrototypeIndexOf(causeStack, '\n at');
12511258
if (causeStackStart !== -1) {
12521259
const causeFrames = StringPrototypeSplit(StringPrototypeSlice(causeStack, causeStackStart + 1), '\n');
Collapse file

‎test/message/util-inspect-error-cause.js‎

Copy file name to clipboardExpand all lines: test/message/util-inspect-error-cause.js
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,9 @@ process.nextTick(() => {
4646
console.log(inspect(cause3));
4747
console.log(inspect(error2));
4848
});
49+
50+
{
51+
const error = new Error('cause that throws');
52+
Reflect.defineProperty(error, 'cause', { get() { throw new Error(); } });
53+
console.log(inspect(error));
54+
}
Collapse file

‎test/message/util-inspect-error-cause.out‎

Copy file name to clipboardExpand all lines: test/message/util-inspect-error-cause.out
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ Error: undefined cause
3333
at * {
3434
[cause]: undefined
3535
}
36+
Error: cause that throws
37+
at *
38+
at *
39+
at *
40+
at *
41+
at *
42+
at *
43+
at * {
44+
[cause]: [Getter]
45+
}
3646
RangeError: New Stack Frames
3747
at *
3848
*[90m at *[39m {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.