Skip to content

Is it possible to make Zod throw custom errors rather than a ZodError? #3280

Discussion options

You must be logged in to vote

Is this what you are looking for?

class CustomException extends Error {
    public name = 'CustomException'

    constructor( public arg: string ) {
        super( `Bad argument: ${ arg }` )
    }
}

const result = z.string().safeParse( 123 )
if ( !result.success ) throw new CustomException( 'arg' )

If you found my answer satisfactory, please consider supporting me. Even a small amount is greatly appreciated. Thanks friend! 🙏
https://github.com/sponsors/JacobWeisenburger

Replies: 3 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by JacobWeisenburger
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants
Converted from issue

This discussion was converted from issue #3279 on February 28, 2024 02:28.