kalempster
kalempster2y ago

Error types in catch block

Hey! I have a mutation that I want to catch if the procedure throws an error, I'd like to get the code of the error ("UNAUTHORIZED", "FORBIDDEN" etc.) but TRCPClientError doesn't have full types (basically I can't access the code property). I saw a generic that you can pass to the error but I don't know what to pass as the generic to get the types.
try {
await mutateAsync(id);
} catch (error) {
if(error instanceof TRPCClientError<>){
//Process the error here if unauthorized regenerate accessToken and retry
}
}
try {
await mutateAsync(id);
} catch (error) {
if(error instanceof TRPCClientError<>){
//Process the error here if unauthorized regenerate accessToken and retry
}
}
Is there any way to do so?
2 Replies
mark salsbery
mark salsbery2y ago
@kalempster What is mutateAsync in your example code? I don’t see any use of tRPC there…
kalempster
kalempster2y ago
I'm sorry, mutateAsync is destructured from a mutation
const { mutateAsync } = trpc.shop.buyShopItem.useMutation({
retry: (failCount, error) => {
if (failCount >= 5) return false;
if (error.data?.code == "UNAUTHORIZED") return false;
return true;
},
});
const { mutateAsync } = trpc.shop.buyShopItem.useMutation({
retry: (failCount, error) => {
if (failCount >= 5) return false;
if (error.data?.code == "UNAUTHORIZED") return false;
return true;
},
});