How to manage custom errors (e.g. custom error codes) in tRPC?

What's the recommended way to add fields to a TRPCError? How do you make that typesafe also on the client (e.g. custom error code is a specific union type). Thanks
N
Nick428d ago
Error Formatting | tRPC
The error formatting in your router will be inferred all the way to your client (& React components)
S
Shooe#6468406428d ago
@Nick Lucas Yeah I read that but it's not clear to me. I currently throw TRPCError directly but in the example it seems like it's wrapping a ZodError in clause (which is unknown so not typesafe) and then using that in a formatter. Is that the flow?
N
Nick428d ago
So it's up to you to detect and format whatever errors you like, obviously Zod is a popular library with tRPC so that makes for a good example, but the example essentially just extends the default shape so TRPCError would also be handled there Whatever you return from errorFormatter will have its type inferred as the error type downstream, so you can define whatever you like really The example does a error.cause instanceof ZodError check to overcome the unknown type of the error, just a limitation of having a global formatter
S
Shooe#6468406428d ago
So for any custom information I should use error.cause similarly?
More Posts
typesafe permissionsHi, So I wanted to infer all the procedures from my router recursively & assign a permission (stringawaiting for procedure & logging the response.Hi, I was wondering if there is a way to handle the return object via the post-middleware's? I know createCaller Dependency Injection in Middleware ctx ?`createCaller` makes it really easy to inject dependencies via anything that's created during the `cbest practices for organizing routes/procedures?i'm trying to find some practices/styles in which people generally define routes with trpc. currentlValidating input inside middleware declaration```js const enforceUserIsCreatorOfEvent = t.middleware(({ ctx, next, input }) => { if (!input.evenFetch errors on stale pagesRecently I have been getting a lot of fetch errors on stale pages, in particular ones that have querHow to use querykeys from react-queryI am trying to implement a search query to an api that i am fetching via a procedure, i also read onDistribute typesafe tRPC Client in an NPM libraryHi ! super fan of trpc over here. We are building a javascript sdk for our API that is essentiallyWebsocket is not defined errorI'm getting a "WebSocket is not defined error" on my next app connected to an express backend. Any i@trpc/server in a non-server environment Error in Azure CIIm trying to add vitest unit tests for my trpc procedures. I followed some examples and on the localHow are people handling authorization?I noticed that with V10, any mentions of `trpc-shield` are gone from the documentation. Also, it onlVitest context router callerHi, Im trying to setup vitest to test trpc. I would like to have a trpc approuter caller to be acces