Handling Effect Errors in tRPC Procedures for Improved Error Handling

Hi guys!

I currently try to incrementally adopt Effect in some of my tRPC procedures that fail more often, so currently my only goes is to improve error handling. I have the program ready but i an not sure how to continue now. For context: Error handling in tRPC relies on throwing like this: throw new TRPCError({ code: "NOT_FOUND" }); where 'code' is a string union type with some pre-defined options. This is important because any error that is thrown in a tRPC procedure that is not a TRPCError (or extends it) is treated as a InternalServerError (if I would use Effect.die for example). That breaks my existing tRPC error formatter function so I try to get around that (in my example redirecting the user to a 404 page in the react app).

So my goal is: If my Effect program finishes with a tagged Effect error (let's call it NotFoundError_Effect), I would like to throw the mentioned TRPCError({ code: "NOT_FOUND" }) . In all other cases for now I am happy with just logging the error with console.err() and throwing a TRPCError({ code: "INTERNAL_SERVER_ERROR" })

Any help would be very appreciated 🙏🏽
Was this page helpful?