functiondjF
tRPC3y ago
6 replies
functiondj

Globally handle specific Error type on backend

Hi, i have a lot of code that's used for both backend and frontend.
That's why i'm not throwing TRPCErrors but custom errors instead. They indicate that the error message should be sent to the client if it arises on the backend (on the frontend this obviously doesn't matter).
Can i set up the tRPC server side in a way where it will catch all errors so i can do something like this?
if (error instanceof DispatchableError) {
  throw new TRPCError({
    code: "BAD_REQUEST",
    message: error.message,
  });
} else {
  throw new TRPCError({
    code: "INTERNAL_SERVER_ERROR",
    message: "Sorry, something went wrong"
  })
}
Solution
Error formatter is correct, you can omit data or enrich data in there for all your needs 🙂
Was this page helpful?