Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: display a present-but-undefined error cause #41247

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/internal/util/inspect.js
Expand Up @@ -1282,7 +1282,7 @@ function formatError(err, constructor, tag, ctx, keys) {

removeDuplicateErrorKeys(ctx, keys, err, stack);

if (err.cause !== undefined &&
if ('cause' in err &&
(keys.length === 0 || !keys.includes('cause'))) {
keys.push('cause');
}
Expand Down
4 changes: 4 additions & 0 deletions test/message/util-inspect-error-cause.js
Expand Up @@ -22,9 +22,13 @@ const cause5 = new Error('Object cause', {
' at Module._compile (node:internal/modules/cjs/loader:827:30)'
}
});
const cause6 = new Error('undefined cause', {
cause: undefined
});

console.log(cause4);
console.log(cause5);
console.log(cause6);

process.nextTick(() => {
const error = new RangeError('New Stack Frames', { cause: cause2 });
Expand Down
10 changes: 10 additions & 0 deletions test/message/util-inspect-error-cause.out
Expand Up @@ -23,6 +23,16 @@ Error: Object cause
' at Module._compile (node:internal/modules/cjs/loader:827:30)'
}
}
Error: undefined cause
at *
at *
at *
at *
at *
at *
at * {
[cause]: undefined
}
RangeError: New Stack Frames
at *
*[90m at *[39m {
Expand Down
6 changes: 6 additions & 0 deletions test/parallel/test-util-inspect.js
Expand Up @@ -665,9 +665,15 @@ assert.strictEqual(util.inspect(-5e-324), '-5e-324');
delete falsyCause1.stack;
const falsyCause2 = new Error(undefined, { cause: null });
falsyCause2.stack = '';
const undefinedCause = new Error('', { cause: undefined });
undefinedCause.stack = '';

assert.strictEqual(util.inspect(falsyCause1), '[Error] { [cause]: false }');
assert.strictEqual(util.inspect(falsyCause2), '[Error] { [cause]: null }');
assert.strictEqual(
util.inspect(undefinedCause),
'[Error] { [cause]: undefined }'
);
}

{
Expand Down