Throwing fastify errors when using fastify adapter
Hello, I'm using
fastifyTRPCPlugin
from @trpc/server/adapters/fastify
and trying to throw errors correctly.
I usually use @fastify-sensible
because I find the API clear, e.g. I can write things like fastify.httpErrors.forbidden()
or fastify.httpErrors.unauthorized
.
However if I throw one of these in a tRPC procedure it seems the error code is always 500, assuming that's because tRPC is intercepting/handling them in some way? What's the best way to "solve" this? Thanks!N
Nick•406d ago
You shouldn’t be throwing fastify errors in services is the brutal answer.
The more helpful answer is you could write a middleware which catches them and wraps them up in a TRPCError as the cause, then you can use an errorFornatter to propagate them in whatever shape you like to the frontend
R
rd•405d ago
Ace, I assumed as much and went ahead and did something similar earlier, but good to have confirmation. It's sometimes difficult to know with adapters which part to prioritise so just wanted some clarification, thanks!
4.8KMembers
View on DiscordMore Posts
Confused about createProxySSGHelpersIf you can use this helper inside of `getServerSideProps` without having `ssr: true` what is the difHow to check if data is being prefetched?What is a method to check if ssg prefetching actually occurs? Through the network tab?How can I access ctx from inside of a procedure?```
const appRouter = t.router({
helloTab: t.procedure.input(z.object({ url: z.string().url() })).Async User Call in `createContext` (context.ts) or in `isAuthed` (in trpc.ts)Hi all! Should I be getting the user in all requests via createContext in the following async call
`'req' of undefined in onError of express middlewareI've recently noticed that I get a bunch of errors regarding req object missing in ctx for onError pMutations and Queries in React Contexts causing unexpected behaviorsIn my organization, we recently moved to put all of our mutations/queries into React Contexts and ouCustom error managementHey peeps! I could've sworn I created a GitHub issue about this, but I must've been dreaming, becauscontext questionWhy are the context examples only showing opinionated examples with next/react auth ? Also i find it