Skip to content

Commit ec03ec3

Browse files
committed
feat: allow calling parseStack with string
1 parent fb63f5f commit ec03ec3

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/__tests__/exception.test.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Simplify } from 'type-fest';
22
import { describe, expect, expectTypeOf, it, test } from 'vitest';
33

4-
import { BaseException, ExceptionBuilder, OutOfRangeException, ValueException } from '../exception.js';
4+
import { BaseException, ExceptionBuilder, OutOfRangeException, parseStack, ValueException } from '../exception.js';
55
import { Err } from '../vendor/neverthrow.js';
66

77
import type { ExceptionConstructor } from '../exception.js';
@@ -189,3 +189,12 @@ describe('OutOfRangeException', () => {
189189
});
190190
});
191191
});
192+
193+
describe('parseStack', () => {
194+
it('should return the same value, in terms of value, whether called with an error or a string', () => {
195+
const error = new Error();
196+
const r1 = parseStack(error);
197+
const r2 = parseStack(error.stack!);
198+
expect(r1).toStrictEqual(r2);
199+
});
200+
});

src/exception.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,11 @@ interface ExceptionLike extends Error, ExceptionOptions {
6363
toString(): string;
6464
}
6565

66-
function parseStack(error: Error): string[] {
67-
return extractStack.lines(cleanStack(error.stack, { pretty: true }));
66+
function parseStack(stack: string | undefined): string[];
67+
function parseStack(error: Error): string[];
68+
function parseStack(errorOrStack: Error | string | undefined): string[] {
69+
const stack = typeof errorOrStack === 'string' ? errorOrStack : errorOrStack?.stack;
70+
return extractStack.lines(cleanStack(stack, { pretty: true }));
6871
}
6972

7073
abstract class BaseException<TParams extends ExceptionParams, TOptions extends ExceptionOptions>

0 commit comments

Comments
 (0)