Skip to content

Commit 74a869b

Browse files
tommiesindresorhus
andauthored
Fix handling of errors with empty stack trace (#19)
Co-authored-by: Sindre Sorhus <[email protected]>
1 parent b8ea765 commit 74a869b

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export default class AggregateError extends Error {
2929
let message = errors
3030
.map(error => {
3131
// The `stack` property is not standardized, so we can't assume it exists
32-
return typeof error.stack === 'string' ? cleanInternalStack(cleanStack(error.stack)) : String(error);
32+
return typeof error.stack === 'string' && error.stack.length > 0 ? cleanInternalStack(cleanStack(error.stack)) : String(error);
3333
})
3434
.join('\n');
3535
message = '\n' + indentString(message, 4);

test.js

+25
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,28 @@ test('gracefully handle Error instances without a stack', t => {
5151
new StacklessError('stackless')
5252
]);
5353
});
54+
55+
test('gracefully handle Error instances with empty stack', t => {
56+
class EmptyStackError extends Error {
57+
constructor(...args) {
58+
super(...args);
59+
this.name = this.constructor.name;
60+
this.stack = '';
61+
}
62+
}
63+
64+
const error = new AggregateError([
65+
new Error('foo'),
66+
new EmptyStackError('emptystack')
67+
]);
68+
69+
console.log(error);
70+
71+
t.regex(error.message, /Error: foo\n {8}at /);
72+
t.regex(error.message, /EmptyStackError: emptystack/);
73+
74+
t.deepEqual([...error.errors], [
75+
new Error('foo'),
76+
new EmptyStackError('emptystack')
77+
]);
78+
});

0 commit comments

Comments
 (0)