justindgm
Vanilla Client Error Handling
What is the right way to handle errors when using the vanilla client?
If I setup a client like so:
And execute a mutation like so:
How should I handle errors? If I try to directly catch when calling the mutate function, the error is typed as
any
Is there a way to type narrow the error at runtime? On the server side, I am using the errorFormatter
to add a custom field to my errors:
My current approach will be to use a runtime validation library like zod to check if this custom field exists on the error
But this is clearly not ideal. Is this the right/best way to approach error handling with the vanilla client, or is there a better approach?10 replies