zod icon indicating copy to clipboard operation
zod copied to clipboard

Better understandable errors

Open benllshua opened this issue 1 year ago • 8 comments

Let's take this simple example:

just put this anywhere in your codebase

z.string().parse(5)

this is the error you will get (as you should):

image

The default error needs to include the location of the error, line of code, and variable name... currently, I think it's really hard to locate errors without writing costume errors for everything (optional: if you don't want it to get to users we can throw an error with one message and return a simpler user-friendly error )

benllshua avatar Feb 18 '24 21:02 benllshua

This seems a bit out of scope for zod

JacobWeisenburger avatar Feb 19 '24 00:02 JacobWeisenburger

Perhaps safeParse would help you. Then you could handle the error however you see fit, rather than throwing and potentially crashing your program.

JacobWeisenburger avatar Feb 20 '24 15:02 JacobWeisenburger

Well I thought more will agree with me but perhaps you are right...

I think my case is good when using zod for situations like env variables and such, I can always parse a costume message manually

benllshua avatar Feb 21 '24 05:02 benllshua

Can I move this to a discussion? Or would you like it to stay as a feature request?

JacobWeisenburger avatar Feb 21 '24 12:02 JacobWeisenburger

Please leave it as a feature request, if other developers doesn't agree with me, close it or move it to discussion s...

benllshua avatar Feb 21 '24 13:02 benllshua

@benllshua The line & column number should appear a few lines above the part that you screenshotted. I agree it would be nice to clean up the stack trace a little if it's possible to do in a runtime-agnostic way.

colinhacks avatar Feb 22 '24 23:02 colinhacks

This seems like a user-skill-issue.

You can add error messages to almost any Zod object, string, array, number, validation rule, etc. Then just safeParse and use the result.data and result.error with correct error message.

m10rten avatar Feb 27 '24 09:02 m10rten

I disagree, this is a very real issue:

The default error needs to include the location of the error, line of code, and variable name... currently, I think it's really hard to locate errors without writing costume errors for everything

It's definitely problematic that all ZodErrors appear to originate inside Zod's source code instead of at the point where .parse() was called.

colinhacks avatar Mar 13 '24 03:03 colinhacks