Custom error management
Hey peeps! I could've sworn I created a GitHub issue about this, but I must've been dreaming, because I cannot find it. Anyway, it's support-related, so I figured it's better to ask here!
I have a large application that has a traditional client/server API and I use Axios to make calls to the server. Now, I'm investigating replacing that with tRPC (because it feels like an amazing developer experience and will quicken development). I have a rather "sophisticated" error management in the application. I have my own custom
In the client, I also have my custom wrapper on top of Axios, that simply catches 400 Bad Request, and converts the JSON back to an
Now, I want to do something similar with tRPC. I see that we have the
1. I don't want to re-write all my code.
2. I think it's a nice abstract layer that the majority of my server side code do not need to be aware of "tRPC".
Does anyone have any ideas on how I can achieve this? I would need some kind of middleware on top of the tRPC procedure that catches my error and converts it into a
Any help or suggestions are appreciated! Thanks in advance.
I have a large application that has a traditional client/server API and I use Axios to make calls to the server. Now, I'm investigating replacing that with tRPC (because it feels like an amazing developer experience and will quicken development). I have a rather "sophisticated" error management in the application. I have my own custom
ApplicationError, which has an errorCode and a custom data object with additional information about the error. Any server-side code in the application can throw this error. A middleware catches this and converts it to a 400 Bad Request with an appropriate JSON response body.In the client, I also have my custom wrapper on top of Axios, that simply catches 400 Bad Request, and converts the JSON back to an
ApplicationError and simply throws it. This means that I can easily catch ApplicationError in my client side code, just as if it was originally thrown on the client.Now, I want to do something similar with tRPC. I see that we have the
TRPCError, but I would love to be able to utilize my ApplicationError. For two reasons:1. I don't want to re-write all my code.
2. I think it's a nice abstract layer that the majority of my server side code do not need to be aware of "tRPC".
Does anyone have any ideas on how I can achieve this? I would need some kind of middleware on top of the tRPC procedure that catches my error and converts it into a
TRPCError. I would also need some kind of filter in my tRPC client code that automatically converts this back into an ApplicationError and throws it.Any help or suggestions are appreciated! Thanks in advance.