asheeshh
asheeshh16mo 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
Mikey16mo 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
asheeshhOP16mo ago
youre asking me to check the error type on the client side?
Mikey
Mikey16mo ago
In the catch, console log out the instanceof for e BEFORE you get into the if statement. @asheeshh
asheeshh
asheeshhOP16mo 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
Mikey16mo ago
Can you share your current code and the console.log for it?
asheeshh
asheeshhOP16mo ago
console log? of what?
Mikey
Mikey16mo ago
This right here...which you said you already did.
asheeshh
asheeshhOP16mo ago
this is the error thrown by trpc
No description
asheeshh
asheeshhOP16mo ago
i just added a console log inside catch to print the error
Mikey
Mikey16mo 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
asheeshhOP16mo ago
nothing inside or outside of the if statements is running the whole catch expression doesnt run when an error is thrown
Mikey
Mikey16mo 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
Mikey16mo ago
This explains how to get the type from a TRPCClientError
Dani;
Dani;16mo ago
Pretty sure you need to await the promise inside the try-catch block