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
10 changes: 10 additions & 0 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -1990,6 +1990,16 @@ function formatError(err, constructor, tag, ctx, keys) {
// If errors is a getter that throws, we ignore the error.
}

// Print the wrapped errors of a SuppressedError.
if (ObjectPrototypeHasOwnProperty(err, 'error') &&
(keys.length === 0 || !ArrayPrototypeIncludes(keys, 'error'))) {
ArrayPrototypePush(keys, 'error');
}
if (ObjectPrototypeHasOwnProperty(err, 'suppressed') &&
(keys.length === 0 || !ArrayPrototypeIncludes(keys, 'suppressed'))) {
ArrayPrototypePush(keys, 'suppressed');
}

stack = improveStack(stack, constructor, name, tag);

// Ignore the error message if it's contained in the stack.
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 @@ -735,6 +735,53 @@ assert.strictEqual(util.inspect(-5e-324), '-5e-324');
Error.stackTraceLimit = stackTraceLimit;
}

{
// The `error` and `suppressed` properties of a SuppressedError should be
// shown during inspection, same as `cause` and AggregateError's `errors`.
const { stackTraceLimit } = Error;
Error.stackTraceLimit = 0;

const disposeError = new Error('dispose error');
const bodyError = new Error('body error');
const suppressedError = new SuppressedError(
disposeError,
bodyError,
'An error was suppressed during disposal',
);

assert.strictEqual(
util.inspect(suppressedError),
'[SuppressedError: An error was suppressed during disposal] ' +
'{\n [error]: [Error: dispose error],\n [suppressed]: [Error: body error]\n}',
);

// Nested SuppressedErrors (multiple failed disposals) must recurse.
const outer = new SuppressedError(
new Error('second dispose error'),
suppressedError,
'outer',
);
assert.strictEqual(
util.inspect(outer),
'[SuppressedError: outer] {\n' +
' [error]: [Error: second dispose error],\n' +
' [suppressed]: [SuppressedError: An error was suppressed during disposal] {\n' +
' [error]: [Error: dispose error],\n' +
' [suppressed]: [Error: body error]\n' +
' }\n' +
'}',
);

const custom = new Error('No own error/suppressed property');
Object.setPrototypeOf(custom, suppressedError);
Comment on lines +775 to +776

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

why not:

Suggested change
const custom = new Error('No own error/suppressed property');
Object.setPrototypeOf(custom, suppressedError);
const custom = new SuppressedError('No own error/suppressed property');

?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the suggestion! I'd like to keep the original version, though — SuppressedError's signature is (error, suppressed, message), so with just one argument that string becomes error, not the message, and both error/suppressed end up as own properties (same as cause above). That means it wouldn't exercise the 'no own property' case this block is meant to test. Happy to tweak the comment or naming if that'd help.

assert.strictEqual(
util.inspect(custom),
'[SuppressedError: No own error/suppressed property]',
);

Error.stackTraceLimit = stackTraceLimit;
}

{
const tmp = Error.stackTraceLimit;
// Force stackTraceLimit = 0 for this test, but make it non-enumerable
Expand Down
Loading