Use onError to change an application error into a TRPCError?
I want to use the onError handler to change any instance of a custom application error into a TRPCError (I want the HTTP Status code to be a 401 rather than a 500). I tried this but it doesn't work...
nor does this
I can set individual properties on the error object like error.message but thought there might be an easier way. Has anybody got a nifty hack for this?
5 Replies
you can do this in https://trpc.io/docs/server/error-formatting
Error Formatting | tRPC
The error formatting in your router will be inferred all the way to your client (& React components)
Thrown errors automatically get wrapped with a TRPCError and sent to the frontend
Like Alex said, an error formatter is probably what you want if you need to manipulate them
ah nice, thanks guys, yep this works.....
See example in my boilerplate repo here https://github.com/JavascriptMick/nuxt3-boilerplate/blob/master/server/trpc/trpc.ts
GitHub
nuxt3-boilerplate/trpc.ts at master · JavascriptMick/nuxt3-boilerpl...
Simple boilerplate for SAAS. Nuxt3, Supabase, OAuth, Prisma, TRPC, Pinia, Stripe, Tailwind, OpenAI - nuxt3-boilerplate/trpc.ts at master · JavascriptMick/nuxt3-boilerplate