Skip to content

Commit 53bc2ce

Browse files
committed
[New] ensure an Error’s cause is displayed
1 parent bc164b6 commit 53bc2ce

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,9 @@ module.exports = function inspect_(obj, options, depth, seen) {
186186
}
187187
if (isError(obj)) {
188188
var parts = arrObjKeys(obj, inspect);
189+
if ('cause' in obj && !isEnumerable.call(obj, 'cause')) {
190+
return '{ [' + String(obj) + '] ' + $join.call($concat.call('[cause]: ' + inspect(obj.cause), parts), ', ') + ' }';
191+
}
189192
if (parts.length === 0) { return '[' + String(obj) + ']'; }
190193
return '{ [' + String(obj) + '] ' + $join.call(parts, ', ') + ' }';
191194
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"aud": "^1.1.5",
1010
"auto-changelog": "^2.3.0",
1111
"core-js": "^2.6.12",
12+
"error-cause": "^1.0.3",
1213
"es-value-fixtures": "^1.2.1",
1314
"eslint": "^8.5.0",
1415
"for-each": "^0.3.3",

test/err.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
var inspect = require('../');
21
var test = require('tape');
2+
var ErrorWithCause = require('error-cause/Error');
3+
4+
var inspect = require('../');
35

46
test('type error', function (t) {
57
t.plan(1);
@@ -14,18 +16,33 @@ test('type error', function (t) {
1416
cerr.message = 'whoa';
1517
cerr['a-b'] = 5;
1618

19+
var withCause = new ErrorWithCause('foo', { cause: 'bar' });
20+
var withCausePlus = new ErrorWithCause('foo', { cause: 'bar' });
21+
withCausePlus.foo = 'bar';
22+
var withUndefinedCause = new ErrorWithCause('foo', { cause: undefined });
23+
var withEnumerableCause = new Error('foo');
24+
withEnumerableCause.cause = 'bar';
25+
1726
var obj = [
1827
new TypeError(),
1928
new TypeError('xxx'),
2029
aerr,
2130
berr,
22-
cerr
31+
cerr,
32+
withCause,
33+
withCausePlus,
34+
withUndefinedCause,
35+
withEnumerableCause
2336
];
2437
t.equal(inspect(obj), '[ ' + [
2538
'[TypeError]',
2639
'[TypeError: xxx]',
2740
'{ [TypeError] foo: 555, bar: [ 1, 2, 3 ] }',
2841
'{ [TypeError: tuv] baz: 555 }',
29-
'{ [SyntaxError: whoa] message: \'whoa\', \'a-b\': 5 }'
42+
'{ [SyntaxError: whoa] message: \'whoa\', \'a-b\': 5 }',
43+
'{ [Error: foo] [cause]: \'bar\' }',
44+
'{ [Error: foo] [cause]: \'bar\', foo: \'bar\' }',
45+
'{ [Error: foo] [cause]: undefined }',
46+
'{ [Error: foo] cause: \'bar\' }'
3047
].join(', ') + ' ]');
3148
});

0 commit comments

Comments
 (0)