Skip to content

Commit

Permalink
refactor: use native format for stack/cause recursion
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed Jul 29, 2024
1 parent bd98d1a commit 0036ad6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
9 changes: 2 additions & 7 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

import { format } from 'node:util';
/**
* Any `Error` compatible with the `NamedError` type signature.
*/
Expand Down Expand Up @@ -35,12 +35,7 @@ export class NamedError extends Error {
}

public get fullStack(): string | undefined {
let stack = this.stack;
const causedStack = this.cause instanceof NamedError ? this.cause?.fullStack ?? this.cause?.stack : undefined;
if (causedStack) {
stack = `${stack ? stack + '\n' : ''}Caused by: ${causedStack}`;
}
return stack;
return format(this);
}
}

Expand Down
20 changes: 10 additions & 10 deletions test/errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ describe('NamedError', () => {
const three = new NamedError('ThreeError', two);
three.stack = `${three.name}:\n at test:3:1`;

expect(three.fullStack).to.equal(
`ThreeError:
at test:3:1
Caused by: TwoError: message two
at test:2:1
Caused by: OneError:
at test:1:1
Caused by: Error: message zero
at test:0:1`
);
[
'NamedError [ThreeError]',
'at test:3:1',
'cause: NamedError [TwoError]: message two',
'at test:2:1',
'cause: NamedError [OneError]',
'at test:1:1',
'cause: Error: message zero',
'at test:0:1',
].map((line) => expect(three.fullStack).to.contain(line));
});
});

Expand Down

0 comments on commit 0036ad6

Please sign in to comment.