Skip to content

Commit 34d0b55

Browse files
authored
fix(parser): include error cause in ParseError (#2774)
1 parent e648f9b commit 34d0b55

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

Diff for: packages/parser/src/errors.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class ParseError extends Error {
77
const errorMessage = options?.cause
88
? `${message}. This error was caused by: ${options?.cause.message}.`
99
: message;
10-
super(errorMessage);
10+
super(errorMessage, options);
1111
this.name = 'ParseError';
1212
}
1313
}

Diff for: packages/parser/tests/unit/envelope.test.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { z } from 'zod';
1+
/**
2+
* Test decorator parser
3+
*
4+
* @group unit/parser
5+
*/
6+
7+
import { z, ZodError } from 'zod';
28
import { Envelope } from '../../src/envelopes/envelope.js';
39
import { ParseError } from '../../src/errors.js';
410

@@ -74,5 +80,12 @@ describe('envelope: ', () => {
7480
Envelope.parse({ name: 123 }, z.object({ name: z.string() }))
7581
).toThrow();
7682
});
83+
it('includes the ZodError as the cause of the ParseError', () => {
84+
try {
85+
Envelope.parse('{"name": "John"}', z.object({ name: z.number() }));
86+
} catch (error) {
87+
expect((error as Error).cause).toBeInstanceOf(ZodError);
88+
}
89+
});
7790
});
7891
});

0 commit comments

Comments
 (0)