santiS
tRPC14mo ago
1 reply
santi

Use TRPCClientError.cause in frontend

Hi all,
My TRPC route can sometimes fail. Say the error is CartValidationError

In the frontend, I want to be able to do:
try {
  await trpcProxyClient.user.cart.addCoupon.mutate({ ...payload, cartOwner })
} except (error) {
  if (error instanceof CartValidationError) {
    // do thing
  } else {
    // do other thing
  }
}

The problem is that the error is of type TRPCClientError. In the backend, I can see that the error.cause is what has the real error (CartValidationError)

Is there a way for me to make the backend return the error in it's original type so I can use instanceOf? If not, can I use error.cause in the frontend? (doesn't seem possible yet)
Was this page helpful?