T
tRPC

Globally handle specific Error type on backend

Globally handle specific Error type on backend

FFunction11/17/2023
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"
})
}
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"
})
}
I'm not sure if errorFormatter is the right thing to use here because this is more of a info/security concern than a formatting one. Because i don't want to leak info to clients about errors i didn't define as DispatchableError.
Solution:
Error formatter is correct, you can omit data or enrich data in there for all your needs 🙂
Jump to solution
Solution
Nnlucas11/17/2023
Error formatter is correct, you can omit data or enrich data in there for all your needs 🙂
Nnlucas11/17/2023
You can also use a middleware to pre-handle errors if you like, but the formatter works well for most projects
FFunction11/17/2023
Ok, thanks a lot!

Looking for more? Join the community!

T
tRPC

Globally handle specific Error type on backend

Join Server
Recommended Posts
How to use token in headers() ?in `_app.tsx` i have this ```import React from 'react'; import { trpcApi, trpcApiClientProvider } fCreating inner context for AWS Lambda Context OptionsHi All, I have been using tRPC for many routes, and I have started to realize I need to call certaiRetry even when disabledI'm not positive if this is a tRPC question or Tanstack.... but I have my query disabled per TanstacMiddleware or request lifecycle hook to run after procedure?Hi, I am using trpc context to create a database client for an incoming request. My understanding iUse RxJS Observable for subscription procedureOn the server, I have an RxJS Observable that I'd like to use for one of my subscription procedures.Individual mutation requests errorHello, quick question regarding the error handling for tRPC So I'm creating kind of a chatroom wherTRPCClientError: fetch failed, using Node 18 in DockerI have both my app and my tRPC server running in Docker via the following docker-compose config: ("nNeed help how to send headers in trpcproject is setup with express on 2 parts,cleint and server i have this pice of cod in client side ``"fetch failed" when buildingHi, we are running into an issue where building our production next app causes "fetch failed" errorsanyone has an example of SSR with React query working without Next js?Can't find a working exampleLooking for an up to date boilerplate for tRPC & Fastify.Looking for an up to date boilerplate for tRPC & Fastify.Can You Provide Guidance on Implementing RBAC and Share GitHub Examples?I am currently exploring the implementation of Role-Based Access Control (RBAC) and am seeking your `useQuery` not working?is it just me or does `useQuery` not work in my next js components? i try to call it and it says cliServer Component Call to TRPC on express backendI have following structure: - Nextjs App folder with both client components and server components -