Skip to content
Merged
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 thread
ljharb marked this conversation as resolved.
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