asheeshh
asheeshh11mo ago

throwing custom errors from mutations

any idea why trpc is ignoring the catch expression? it just throws its own error about unique constraint violation
No description
14 Replies
Mikey
Mikey11mo ago
@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.
asheeshh
asheeshh11mo ago
youre asking me to check the error type on the client side?
Mikey
Mikey11mo ago
In the catch, console log out the instanceof for e BEFORE you get into the if statement. @asheeshh
asheeshh
asheeshh11mo ago
oh actually the thing is my catch expression is completely ignored by trpc nothing inside catch runs already tried console logging stuff inside it
Mikey
Mikey11mo ago
Can you share your current code and the console.log for it?
asheeshh
asheeshh11mo ago
console log? of what?
Mikey
Mikey11mo ago
This right here...which you said you already did.
asheeshh
asheeshh11mo ago
this is the error thrown by trpc
No description
asheeshh
asheeshh11mo ago
i just added a console log inside catch to print the error
Mikey
Mikey11mo ago
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.
asheeshh
asheeshh11mo ago
nothing inside or outside of the if statements is running the whole catch expression doesnt run when an error is thrown
Mikey
Mikey11mo ago
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.
Mikey
Mikey11mo ago
This explains how to get the type from a TRPCClientError
Dani;
Dani;11mo ago
Pretty sure you need to await the promise inside the try-catch block