Theo Gravity
Theo Gravity14mo ago

How do I log thrown errors from the query/ mutation on the server side globally?

I was surprised to find that the server servicing tRPC requests do not log any thrown errors from a query / mutation to the console by default (but the client does capture errors). I want to be able to log all errors on the server side. How would I do this? This page doesn't really give me any clues as I don't know what trpcNext.createNextApiHandler refers to: https://trpc.io/docs/server/error-handling#handling-errors I am using the express adapter with tRPC. I do not want to have to attach an error handling function to each query / mutation I define.
Error Handling | tRPC
Whenever an error occurs in a procedure, tRPC responds to the client with an object that includes an "error" property. This property contains all the information that you need to handle the error in the client.
5 Replies
Nick
Nick14mo ago
All the adapters have an onError callback so you can use that Look at the docs for your adapter
Theo Gravity
Theo Gravity14mo ago
It doesn't offer anything around that: https://trpc.io/docs/server/adapters/express
Theo Gravity
Theo Gravity14mo ago
GitHub
trpc/server.ts at main · trpc/trpc
🧙‍♀️ Move Fast and Break Nothing. End-to-end typesafe APIs made easy. - trpc/server.ts at main · trpc/trpc
Nick
Nick14mo ago
It might not be directly documented in this case, but it’s there and your editor autocomplete is all the docs you really need, get your editor to list all the options you can pass to the adapter
Theo Gravity
Theo Gravity14mo ago
Typing on did help in the autocomplete for the adapter, thanks
trpcExpress.createExpressMiddleware({
router: appRouter,
createContext,
onError({ error, ctx }) {

},
trpcExpress.createExpressMiddleware({
router: appRouter,
createContext,
onError({ error, ctx }) {

},