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: make sure error causes of any type may be inspected #41097

Closed
Closed
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
5 changes: 3 additions & 2 deletions lib/internal/util/inspect.js
Expand Up @@ -1198,7 +1198,7 @@ function getStackFrames(ctx, err, stack) {
const frames = stack.split('\n');

// Remove stack frames identical to frames in cause.
if (err.cause) {
if (err.cause && isError(err.cause)) {
const causeStack = getStackString(err.cause);
const causeStackStart = causeStack.indexOf('\n at');
if (causeStackStart !== -1) {
Expand Down Expand Up @@ -1265,7 +1265,8 @@ function formatError(err, constructor, tag, ctx, keys) {

removeDuplicateErrorKeys(ctx, keys, err, stack);

if (err.cause && (keys.length === 0 || !keys.includes('cause'))) {
if (err.cause !== undefined &&
(keys.length === 0 || !keys.includes('cause'))) {
keys.push('cause');
}

Expand Down
13 changes: 13 additions & 0 deletions test/message/util-inspect-error-cause.js
Expand Up @@ -13,6 +13,19 @@ const cause2 = new FoobarError('Individual message', { cause: cause1 });
cause2.extraProperties = 'Yes!';
const cause3 = new Error('Stack causes', { cause: cause2 });

const cause4 = new Error('Number error cause', { cause: 42 });
const cause5 = new Error('Object cause', {
cause: {
message: 'Unique',
name: 'Error',
stack: 'Error: Unique\n' +
' at Module._compile (node:internal/modules/cjs/loader:827:30)'
}
});

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

process.nextTick(() => {
const error = new RangeError('New Stack Frames', { cause: cause2 });
const error2 = new RangeError('New Stack Frames', { cause: cause3 });
Expand Down
25 changes: 25 additions & 0 deletions test/message/util-inspect-error-cause.out
@@ -1,3 +1,28 @@
Error: Number error cause
at *
at *
at *
at *
at *
at *
at * {
[cause]: 42
}
Error: Object cause
at *
at *
at *
at *
at *
at *
at * {
[cause]: {
BridgeAR marked this conversation as resolved.
Show resolved Hide resolved
message: 'Unique',
name: 'Error',
stack: 'Error: Unique\n' +
' at Module._compile (node:internal/modules/cjs/loader:827:30)'
}
}
RangeError: New Stack Frames
at *
*[90m at *[39m {
Expand Down
10 changes: 10 additions & 0 deletions test/parallel/test-util-inspect.js
Expand Up @@ -660,6 +660,16 @@ assert.strictEqual(util.inspect(-5e-324), '-5e-324');
assert(ex.includes('[message]'));
}

{
const falsyCause1 = new Error('', { cause: false });
delete falsyCause1.stack;
const falsyCause2 = new Error(undefined, { cause: null });
falsyCause2.stack = '';

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

{
const tmp = Error.stackTraceLimit;
Error.stackTraceLimit = 0;
Expand Down