There is no way to determine that an error thrown is a `ZodError`
See original GitHub issueIn Zod v1, to determine whether a thrown error is an error thrown by Zod, I did if (error.constructor.name === 'ZodError') {...}
.
In Zod v3, this is not possible anymore (at least when importing the module using ESM), because you minimize the source code, and the name of the constructor becomes "n"
😬.
Would love it if there was a way to know if the error is a Zod error. As a suggestion, have the code
values all start with a ZOD_ERROR
prefix, or have an isZodError
field in the error, or perhaps even ensure that the constructor name will still be ZodError
.
Currently, I’m working around this by a heuristic: checking that the error has a path
property that is an array. 😱
BTW, kudos for adding ESM support! 🎉
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:7 (3 by maintainers)
Top Results From Across the Web
zod/ERROR_HANDLING.md at master · colinhacks/zod - GitHub
This guide explains Zod's internal error handling system, and the various ways you can customize it for your purposes. ZodError. All validation errors...
Read more >Error Handling in Zod - Deno
This guide explains Zod's internal error handling system, and the various ways you can customize it for your purposes. ZodError. All validation errors...
Read more >How do I fix this zod error I am getting while deploying ...
I believe to have found where the error is in my code, I just don't know how to fix it. It is the...
Read more >zod-error - npm
Options to customize the code component of the error message. delimiter? DelimiterOptions, Set the delimiter between error messages and between ...
Read more >Errors - discord.js Guide
There is no doubt that you have encountered errors while making bots. ... JavaScript errors are thrown by node itself or by discord.js....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I now manually set
this.name = "ZodError";
in the ZodError constructor so I believe your old approach should work again in zod@3.7+@colinhacks I actually don’t remember 😊, but the code spefically uses
error.constructor.name
. But I did have a problem. My guess is that I used Zod in another realm (e.g. a Node.js VM), it threw the exception, and I caught it in the main code.But as I said above, once I realized I can use
instanceof
, I’m fine with not fixing this. I’ll close it.And, again, thanks for the ESM support!