Skip to content

Commit 6baddcf

Browse files
Ruben Bridgewateraddaleax
authored andcommitted
util: only inspect error properties that are not visible otherwise
Inspecting errors results in duplicated information in case an error is created with enumerable `name`, `message` or `stack` properties. In that case, check if the output already contains that information and prevent listing that property. This reduces the noise as receiver. PR-URL: #32327 Reviewed-By: Anto Aravinth <[email protected]>
1 parent e20b4f9 commit 6baddcf

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

lib/internal/util/inspect.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -890,7 +890,7 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
890890
return ctx.stylize(base, 'date');
891891
}
892892
} else if (isError(value)) {
893-
base = formatError(value, constructor, tag, ctx);
893+
base = formatError(value, constructor, tag, ctx, keys);
894894
if (keys.length === 0 && protoProps === undefined)
895895
return base;
896896
} else if (isAnyArrayBuffer(value)) {
@@ -1083,11 +1083,23 @@ function getFunctionBase(value, constructor, tag) {
10831083
return base;
10841084
}
10851085

1086-
function formatError(err, constructor, tag, ctx) {
1086+
function formatError(err, constructor, tag, ctx, keys) {
10871087
const name = err.name != null ? String(err.name) : 'Error';
10881088
let len = name.length;
10891089
let stack = err.stack ? String(err.stack) : ErrorPrototypeToString(err);
10901090

1091+
// Do not "duplicate" error properties that are already included in the output
1092+
// otherwise.
1093+
if (!ctx.showHidden && keys.length !== 0) {
1094+
for (const name of ['name', 'message', 'stack']) {
1095+
const index = keys.indexOf(name);
1096+
// Only hide the property in case it's part of the original stack
1097+
if (index !== -1 && stack.includes(err[name])) {
1098+
keys.splice(index, 1);
1099+
}
1100+
}
1101+
}
1102+
10911103
// A stack trace may contain arbitrary data. Only manipulate the output
10921104
// for "regular errors" (errors that "look normal") for now.
10931105
if (constructor === null ||

test/parallel/test-util-inspect.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,28 @@ assert.strictEqual(util.inspect(-5e-324), '-5e-324');
697697
Error.stackTraceLimit = tmp;
698698
}
699699

700+
// Prevent enumerable error properties from being printed.
701+
{
702+
let err = new Error();
703+
err.message = 'foobar';
704+
let out = util.inspect(err).split('\n');
705+
assert.strictEqual(out[0], 'Error: foobar');
706+
assert(out[out.length - 1].startsWith(' at '));
707+
// Reset the error, the stack is otherwise not recreated.
708+
err = new Error();
709+
err.message = 'foobar';
710+
err.name = 'Unique';
711+
Object.defineProperty(err, 'stack', { value: err.stack, enumerable: true });
712+
out = util.inspect(err).split('\n');
713+
assert.strictEqual(out[0], 'Unique: foobar');
714+
assert(out[out.length - 1].startsWith(' at '));
715+
err.name = 'Baz';
716+
out = util.inspect(err).split('\n');
717+
assert.strictEqual(out[0], 'Unique: foobar');
718+
assert.strictEqual(out[out.length - 2], " name: 'Baz'");
719+
assert.strictEqual(out[out.length - 1], '}');
720+
}
721+
700722
// Doesn't capture stack trace.
701723
{
702724
function BadCustomError(msg) {

0 commit comments

Comments
 (0)