joe_and_duke
joe_and_duke15mo ago

tRPC onError

I am using tRPC with Fastify and would like to be able to report issues when we get an error. https://trpc.io/docs/server/error-handling#handling-errors It mentions we have a onError function, but I only see it inside: createNextApiHandler How can I get access to this not using Next? Am I just missing something?
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.
3 Replies
arn4v
arn4v15mo ago
Most (if not all, not sure) server options are the same across all adapters. You can access it when you create the trpc middleware (in case of express) or register the trpc plugin (in case of fastify).
Mugetsu
Mugetsu15mo ago
OnError handler should be setup with your Fastify adapter for tRPC. https://trpc.io/docs/server/adapters/fastify#create-fastify-server
...

server.register(fastifyTRPCPlugin, {
prefix: '/trpc',
trpcOptions: {
router: appRouter,
createContext.
onError <------ HERE
},
});

...
...

server.register(fastifyTRPCPlugin, {
prefix: '/trpc',
trpcOptions: {
router: appRouter,
createContext.
onError <------ HERE
},
});

...
joe_and_duke
joe_and_duke15mo ago
Thanks, missed that!