Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/internal/assert/assertion_error.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const {
ObjectDefineProperty,
ObjectGetPrototypeOf,
ObjectPrototypeHasOwnProperty,
SafeSet,
String,
StringPrototypeRepeat,
StringPrototypeSlice,
Expand Down Expand Up @@ -42,7 +43,7 @@ const kReadableOperator = {
const kMaxShortStringLength = 12;
const kMaxLongStringLength = 512;

const kMethodsWithCustomMessageDiff = ['deepStrictEqual', 'strictEqual', 'partialDeepStrictEqual'];
const kMethodsWithCustomMessageDiff = new SafeSet(['deepStrictEqual', 'strictEqual', 'partialDeepStrictEqual']);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems unrelated

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See PR description

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should its own PR IMO, as is it makes the git blame harder to use, and so it can be reverted / benchmarked on its own if need be


function copyError(source) {
const target = ObjectAssign(
Expand Down Expand Up @@ -263,7 +264,7 @@ class AssertionError extends Error {
if (isErrorStackTraceLimitWritable()) Error.stackTraceLimit = 0;

if (message != null) {
if (kMethodsWithCustomMessageDiff.includes(operator)) {
if (kMethodsWithCustomMessageDiff.has(operator)) {
super(createErrDiff(actual, expected, operator, message, diff));
} else {
super(String(message));
Expand All @@ -283,7 +284,7 @@ class AssertionError extends Error {
expected = copyError(expected);
}

if (kMethodsWithCustomMessageDiff.includes(operator)) {
if (kMethodsWithCustomMessageDiff.has(operator)) {
super(createErrDiff(actual, expected, operator, message, diff));
} else if (operator === 'notDeepStrictEqual' ||
operator === 'notStrictEqual') {
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -1955,15 +1955,15 @@ 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');
}

// 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');
}
Expand Down
47 changes: 47 additions & 0 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading