Skip to content

Commit

Permalink
fix: attach original cause in xception
Browse files Browse the repository at this point in the history
  • Loading branch information
alvis committed May 17, 2024
1 parent f9f0b61 commit 13055bc
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
2 changes: 1 addition & 1 deletion source/xception.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default function xception(
exception: unknown, // string, error, Xception, { message: string, ...}
options?: Options,
): Xception {
const cause = exception;
const cause = exception instanceof Xception ? exception.cause : exception;

// when options.factory is provided, it's used to create Xception instances; otherwise, the default constructor is used
const factory =
Expand Down
6 changes: 5 additions & 1 deletion spec/xception.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ describe('fn:xceptionalize', () => {
expect(xceptionalizedError.message).toEqual('test');
expect(xceptionalizedError.stack).toEqual(error.stack);
expect(xceptionalizedError[$meta]).toEqual({});
expect(xceptionalizedError.cause).toEqual(error);
}
});

Expand All @@ -47,13 +48,16 @@ describe('fn:xceptionalize', () => {
expect(xceptionalizedError.name).toEqual('Xception');
expect(xceptionalizedError.message).toEqual('test');
expect(xceptionalizedError[$meta]).toEqual({});
expect(xceptionalizedError.cause).toEqual({ message: 'test' });
});

it('should ignore any xception instance', () => {
const xceptionError = new Xception('test');
const cause = new Error('test');
const xceptionError = new Xception('test', { cause });
const xceptionalizedError = xception(xceptionError);

expect(xceptionalizedError).toEqual(xceptionError);
expect(xceptionalizedError.cause).toEqual(cause);
});

it('should throw an exception if the input is not a string, an error or an Xception', () => {
Expand Down

0 comments on commit 13055bc

Please sign in to comment.