question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

There is no way to determine that an error thrown is a `ZodError`

See original GitHub issue

In 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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
colinhackscommented, Aug 1, 2021

I now manually set this.name = "ZodError"; in the ZodError constructor so I believe your old approach should work again in zod@3.7+

2reactions
giltayarcommented, Jul 5, 2021

@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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found