Skip to content

Commit 848d81c

Browse files
Ruben Bridgewatertargos
Ruben Bridgewater
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 2e015e5 commit 848d81c

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

lib/internal/util/inspect.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,7 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
885885
return ctx.stylize(base, 'date');
886886
}
887887
} else if (isError(value)) {
888-
base = formatError(value, constructor, tag, ctx);
888+
base = formatError(value, constructor, tag, ctx, keys);
889889
if (keys.length === 0 && protoProps === undefined)
890890
return base;
891891
} else if (isAnyArrayBuffer(value)) {
@@ -1078,11 +1078,23 @@ function getFunctionBase(value, constructor, tag) {
10781078
return base;
10791079
}
10801080

1081-
function formatError(err, constructor, tag, ctx) {
1081+
function formatError(err, constructor, tag, ctx, keys) {
10821082
const name = err.name != null ? String(err.name) : 'Error';
10831083
let len = name.length;
10841084
let stack = err.stack ? String(err.stack) : ErrorPrototypeToString(err);
10851085

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

test/parallel/test-util-inspect.js

+22
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,28 @@ assert.strictEqual(util.inspect(-5e-324), '-5e-324');
696696
Error.stackTraceLimit = tmp;
697697
}
698698

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

0 commit comments

Comments
 (0)