Skip to content

Commit 26b7fd2

Browse files
authored
util: limit inspect to only show own properties
`Error`'s `cause` and `errors` properties would be visible even if these were not own properties. This is changed to align with all other parts of the `inspect` handling. Fixes: #60717 PR-URL: #61032 Reviewed-By: Jordan Harband <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
1 parent d7e4108 commit 26b7fd2

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

lib/internal/util/inspect.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1961,15 +1961,15 @@ function formatError(err, constructor, tag, ctx, keys) {
19611961
}
19621962
name ??= 'Error';
19631963

1964-
if ('cause' in err &&
1964+
if (ObjectPrototypeHasOwnProperty(err, 'cause') &&
19651965
(keys.length === 0 || !ArrayPrototypeIncludes(keys, 'cause'))) {
19661966
ArrayPrototypePush(keys, 'cause');
19671967
}
19681968

19691969
// Print errors aggregated into AggregateError
19701970
try {
19711971
const errors = err.errors;
1972-
if (ArrayIsArray(errors) &&
1972+
if (ArrayIsArray(errors) && ObjectPrototypeHasOwnProperty(err, 'errors') &&
19731973
(keys.length === 0 || !ArrayPrototypeIncludes(keys, 'errors'))) {
19741974
ArrayPrototypePush(keys, 'errors');
19751975
}

test/parallel/test-util-inspect.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,53 @@ assert.strictEqual(util.inspect(-5e-324), '-5e-324');
688688
);
689689
}
690690

691+
{
692+
// No own errors or cause property.
693+
const { stackTraceLimit } = Error;
694+
Error.stackTraceLimit = 0;
695+
696+
const e1 = new Error('e1');
697+
const e2 = new TypeError('e2');
698+
const e3 = false;
699+
700+
const errors = [e1, e2, e3];
701+
const aggregateError = new AggregateError(errors, 'Foobar');
702+
703+
assert.deepStrictEqual(aggregateError.errors, errors);
704+
assert.strictEqual(
705+
util.inspect(aggregateError),
706+
'[AggregateError: Foobar] {\n [errors]: [ [Error: e1], [TypeError: e2], false ]\n}'
707+
);
708+
709+
710+
const custom = new Error('No own errors property');
711+
Object.setPrototypeOf(custom, aggregateError);
712+
713+
assert.strictEqual(
714+
util.inspect(custom),
715+
'[AggregateError: No own errors property]'
716+
);
717+
718+
const cause = [new Error('cause')];
719+
const causeError = new TypeError('Foobar', { cause: [new Error('cause')] });
720+
721+
assert.strictEqual(
722+
util.inspect(causeError),
723+
'[TypeError: Foobar] { [cause]: [ [Error: cause] ] }'
724+
);
725+
726+
const custom2 = new Error('No own cause property');
727+
Object.setPrototypeOf(custom2, causeError);
728+
729+
assert.deepStrictEqual(custom2.cause, cause);
730+
assert.strictEqual(
731+
util.inspect(custom2),
732+
'[TypeError: No own cause property]'
733+
);
734+
735+
Error.stackTraceLimit = stackTraceLimit;
736+
}
737+
691738
{
692739
const tmp = Error.stackTraceLimit;
693740
// Force stackTraceLimit = 0 for this test, but make it non-enumerable

0 commit comments

Comments
 (0)