Skip to content

Commit

Permalink
fix(parser): include error cause in ParseError (#2774)
Browse files Browse the repository at this point in the history
  • Loading branch information
tusharf5 authored Jul 15, 2024
1 parent e648f9b commit 34d0b55
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/parser/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class ParseError extends Error {
const errorMessage = options?.cause
? `${message}. This error was caused by: ${options?.cause.message}.`
: message;
super(errorMessage);
super(errorMessage, options);
this.name = 'ParseError';
}
}
Expand Down
15 changes: 14 additions & 1 deletion packages/parser/tests/unit/envelope.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { z } from 'zod';
/**
* Test decorator parser
*
* @group unit/parser
*/

import { z, ZodError } from 'zod';
import { Envelope } from '../../src/envelopes/envelope.js';
import { ParseError } from '../../src/errors.js';

Expand Down Expand Up @@ -74,5 +80,12 @@ describe('envelope: ', () => {
Envelope.parse({ name: 123 }, z.object({ name: z.string() }))
).toThrow();
});
it('includes the ZodError as the cause of the ParseError', () => {
try {
Envelope.parse('{"name": "John"}', z.object({ name: z.number() }));
} catch (error) {
expect((error as Error).cause).toBeInstanceOf(ZodError);
}
});
});
});

0 comments on commit 34d0b55

Please sign in to comment.