util: display a present-but-undefined error cause

See https://github.com/nodejs/node/pull/41097#issuecomment-997055761

PR-URL: https://github.com/nodejs/node/pull/41247
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
pull/41281/head
Jordan Harband 2021-12-21 15:46:36 -08:00 committed by GitHub
parent 0264f2395f
commit 8d1f13b376
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 1 deletions

View File

@ -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');
}

View File

@ -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 });

View File

@ -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 {

View File

@ -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 }'
);
}
{