mirror of
https://github.com/zebrajr/node.git
synced 2026-01-15 12:15:26 +00:00
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: https://github.com/nodejs/node/issues/60717 PR-URL: https://github.com/nodejs/node/pull/61032 Reviewed-By: Jordan Harband <ljharb@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
This commit is contained in:
committed by
GitHub
parent
d7e4108bc1
commit
26b7fd2009
@@ -1961,7 +1961,7 @@ function formatError(err, constructor, tag, ctx, keys) {
|
||||
}
|
||||
name ??= 'Error';
|
||||
|
||||
if ('cause' in err &&
|
||||
if (ObjectPrototypeHasOwnProperty(err, 'cause') &&
|
||||
(keys.length === 0 || !ArrayPrototypeIncludes(keys, 'cause'))) {
|
||||
ArrayPrototypePush(keys, 'cause');
|
||||
}
|
||||
@@ -1969,7 +1969,7 @@ function formatError(err, constructor, tag, ctx, keys) {
|
||||
// Print errors aggregated into AggregateError
|
||||
try {
|
||||
const errors = err.errors;
|
||||
if (ArrayIsArray(errors) &&
|
||||
if (ArrayIsArray(errors) && ObjectPrototypeHasOwnProperty(err, 'errors') &&
|
||||
(keys.length === 0 || !ArrayPrototypeIncludes(keys, 'errors'))) {
|
||||
ArrayPrototypePush(keys, 'errors');
|
||||
}
|
||||
|
||||
@@ -688,6 +688,53 @@ assert.strictEqual(util.inspect(-5e-324), '-5e-324');
|
||||
);
|
||||
}
|
||||
|
||||
{
|
||||
// No own errors or cause property.
|
||||
const { stackTraceLimit } = Error;
|
||||
Error.stackTraceLimit = 0;
|
||||
|
||||
const e1 = new Error('e1');
|
||||
const e2 = new TypeError('e2');
|
||||
const e3 = false;
|
||||
|
||||
const errors = [e1, e2, e3];
|
||||
const aggregateError = new AggregateError(errors, 'Foobar');
|
||||
|
||||
assert.deepStrictEqual(aggregateError.errors, errors);
|
||||
assert.strictEqual(
|
||||
util.inspect(aggregateError),
|
||||
'[AggregateError: Foobar] {\n [errors]: [ [Error: e1], [TypeError: e2], false ]\n}'
|
||||
);
|
||||
|
||||
|
||||
const custom = new Error('No own errors property');
|
||||
Object.setPrototypeOf(custom, aggregateError);
|
||||
|
||||
assert.strictEqual(
|
||||
util.inspect(custom),
|
||||
'[AggregateError: No own errors property]'
|
||||
);
|
||||
|
||||
const cause = [new Error('cause')];
|
||||
const causeError = new TypeError('Foobar', { cause: [new Error('cause')] });
|
||||
|
||||
assert.strictEqual(
|
||||
util.inspect(causeError),
|
||||
'[TypeError: Foobar] { [cause]: [ [Error: cause] ] }'
|
||||
);
|
||||
|
||||
const custom2 = new Error('No own cause property');
|
||||
Object.setPrototypeOf(custom2, causeError);
|
||||
|
||||
assert.deepStrictEqual(custom2.cause, cause);
|
||||
assert.strictEqual(
|
||||
util.inspect(custom2),
|
||||
'[TypeError: No own cause property]'
|
||||
);
|
||||
|
||||
Error.stackTraceLimit = stackTraceLimit;
|
||||
}
|
||||
|
||||
{
|
||||
const tmp = Error.stackTraceLimit;
|
||||
// Force stackTraceLimit = 0 for this test, but make it non-enumerable
|
||||
|
||||
Reference in New Issue
Block a user