Skip to content

Commit

Permalink
Fix handling of errors with empty stack trace (#19)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
tommie and sindresorhus committed May 8, 2022
1 parent b8ea765 commit 74a869b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default class AggregateError extends Error {
let message = errors
.map(error => {
// The `stack` property is not standardized, so we can't assume it exists
return typeof error.stack === 'string' ? cleanInternalStack(cleanStack(error.stack)) : String(error);
return typeof error.stack === 'string' && error.stack.length > 0 ? cleanInternalStack(cleanStack(error.stack)) : String(error);
})
.join('\n');
message = '\n' + indentString(message, 4);
Expand Down
25 changes: 25 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,28 @@ test('gracefully handle Error instances without a stack', t => {
new StacklessError('stackless')
]);
});

test('gracefully handle Error instances with empty stack', t => {
class EmptyStackError extends Error {
constructor(...args) {
super(...args);
this.name = this.constructor.name;
this.stack = '';
}
}

const error = new AggregateError([
new Error('foo'),
new EmptyStackError('emptystack')
]);

console.log(error);

t.regex(error.message, /Error: foo\n {8}at /);
t.regex(error.message, /EmptyStackError: emptystack/);

t.deepEqual([...error.errors], [
new Error('foo'),
new EmptyStackError('emptystack')
]);
});

0 comments on commit 74a869b

Please sign in to comment.