Skip to content

Commit ab97c2a

Browse files
committed
util: safely inspect getter errors whose message throws
1 parent cdc3ca8 commit ab97c2a

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

lib/internal/util/inspect.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2557,7 +2557,14 @@ function formatProperty(ctx, value, recurseTimes, key, type, desc,
25572557
}
25582558
ctx.indentationLvl -= 2;
25592559
} catch (err) {
2560-
const message = `<Inspection threw (${err.message})>`;
2560+
let messageSuffix;
2561+
try {
2562+
// Error message itself may be a getter
2563+
messageSuffix = ` (${err.message})`;
2564+
} catch {
2565+
messageSuffix = '';
2566+
}
2567+
const message = `<Inspection threw${messageSuffix}>`;
25612568
str = `${s(`[${label}:`, sp)} ${message}${s(']', sp)}`;
25622569
}
25632570
} else {

test/parallel/test-util-inspect.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2551,6 +2551,23 @@ assert.strictEqual(
25512551
"'foobar', { x: 1 } },\n inc: [Getter: NaN]\n}");
25522552
}
25532553

2554+
// Test for property getter throwing an error with a bad message.
2555+
{
2556+
const error = {
2557+
// The message itself is a getter that throws
2558+
get message() { throw new Error('Oops'); }
2559+
};
2560+
2561+
const thrower = {
2562+
get foo() { throw error; }
2563+
};
2564+
2565+
assert.strictEqual(
2566+
inspect(thrower, { getters: true }),
2567+
'{ foo: [Getter: <Inspection threw>] }'
2568+
);
2569+
}
2570+
25542571
// Check compact number mode.
25552572
{
25562573
let obj = {

0 commit comments

Comments
 (0)