tRPCttRPC
Powered by
santiS
tRPC•15mo ago•
1 reply
santi

Use TRPCClientError.cause in frontend

Hi all,
My TRPC route can sometimes fail. Say the error is
CartValidationError
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
  }
}
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
error.cause
is what has the real error (
CartValidationError
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)
tRPCJoin
Move Fast & Break Nothing. End-to-end typesafe APIs made easy.
5,015Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

FormData TRPCClientError
vpjmVvpjm / ❓-help
12mo ago
TRPCClientError: Cannot use 'in' operator to search for 'error' in null
bos | endgames.ioBbos | endgames.io / ❓-help
2y ago
Subscription error: TRPCClientError: Subscriptions should use wsLink
jaacsenJjaacsen / ❓-help
2y ago
Unhandled runtime TRPCClientError
Answer OverflowAAnswer Overflow / ❓-help
3y ago