Skip to content

Commit

Permalink
Adapt stackWithCauses() to node.js output
Browse files Browse the repository at this point in the history
  • Loading branch information
voxpelli committed Mar 2, 2022
1 parent 1a93b68 commit 38e1958
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
10 changes: 8 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,17 @@ const _stackWithCauses = (err, seen) => {

const cause = getErrorCause(err);

// TODO: Follow up in https://github.com/nodejs/node/issues/38725#issuecomment-920309092 on how to log stuff

if (cause) {
seen.add(err);
return (stack + '\ncaused by: ' + _stackWithCauses(cause, seen));
} else if (
// @ts-ignore
err.cause
) {
return (stack + '\ncaused by: ' + JSON.stringify(
// @ts-ignore
err.cause
));
} else {
return stack;
}
Expand Down
33 changes: 33 additions & 0 deletions test/stack.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,37 @@ describe('stackWithCauses()', () => {
const result = stackWithCauses(err);
result.should.equal('xyz789\ncaused by: abc123\ncaused by: xyz789\ncauses have become circular...');
});

it('should append non-Error string causes to the end of the cause trail', () => {
const cause = 'string cause';

const err = new ErrorWithCause('foo', { cause });
err.stack = 'xyz789';

const result = stackWithCauses(err);
should.exist(result);
result.should.equal('xyz789\ncaused by: "string cause"');
});

it('should append non-Error number causes to the end of the cause trail', () => {
const cause = 123;

const err = new ErrorWithCause('foo', { cause });
err.stack = 'xyz789';

const result = stackWithCauses(err);
should.exist(result);
result.should.equal('xyz789\ncaused by: 123');
});

it('should append non-Error object causes to the end of the cause trail', () => {
const cause = { name: 'TypeError', message: 'foo' };

const err = new ErrorWithCause('foo', { cause });
err.stack = 'xyz789';

const result = stackWithCauses(err);
should.exist(result);
result.should.equal('xyz789\ncaused by: {"name":"TypeError","message":"foo"}');
});
});

0 comments on commit 38e1958

Please sign in to comment.