throwing custom errors from mutations
any idea why trpc is ignoring the catch expression?
it just throws its own error about unique constraint violation
14 Replies
@asheeshh try checking the type of your error. You may need to do an if check against TRPC error first and then throw a new error based on what you're experiencing.
youre asking me to check the error type on the client side?
In the catch, console log out the instanceof for e BEFORE you get into the if statement.
@asheeshh
oh actually the thing is my catch expression is completely ignored by trpc
nothing inside catch runs
already tried console logging stuff inside it
Can you share your current code and the console.log for it?
console log?
of what?
This right here...which you said you already did.
this is the error thrown by trpc
i just added a console log inside catch to print the error
Put this before your if statement in the catch block.
console.log(e instanceof TRPCClientError)
If this is true, then that means you will never run the code in your if statement.
nothing inside or outside of the if statements is running
the whole catch expression doesnt run when an error is thrown
Without seeing the rest of your code, it's difficult to help you. But try looking at this: https://trpc.io/docs/client/vanilla/infer-types#infer-trpcclienterror-types
Inferring Types | tRPC
It is often useful to access the types of your API within your clients. For this purpose, you are able to infer the types contained in your AppRouter.
This explains how to get the type from a TRPCClientError
Pretty sure you need to
await
the promise inside the try-catch block