Support Post-processing thrown Errors #6008
rubber-duck-software
started this conversation in
Proposals
Replies: 1 comment
-
Hey @sergiodxa - If I'm not mistaken, you have the most context for error handling in Remix. Can I get your eyes on this so #5957 can move forward? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Proposal
Add support for a global error handler which can catch errors thrown in loaders and actions and transform the error into a response.
Background
My motivating use case is Custom Error types. We have error types such as
ValidationError
,PaymentRequired
,ConflictError
,InsufficientPermissions
etc. We use these in shared logic in our monorepo, since the shared logic might be consumed by Remix, by an Express backend, or by any number of consumers. Custom Error types are a relatively common practice for exactly these types of situations.In Remix, if I want to convert these thrown errors into their appropriate http status codes, I need to wrap every loader and action in a try/catch block where the catch block transforms the error if appropriate.
So, while it is possible to use these custom error types with Remix, the developer ergonomics are pretty poor. Even worse, the error handling logic cannot be easily abstracted since higher order functions are not allowed (https://remix.run/docs/en/1.15.0/guides/constraints#higher-order-functions)
Proposal Details
I propose that a top level error handler is added to Remix such that errors thrown in loaders or actions can be caught and the appropriate response be transformed.
The desire to handle custom error types is relatively common. This would pair nicely with #5957 whose objective is to
Possible APIs
This is just my initial thought, but we could add a named export to
entry.server
which is called on every request and allows transforming any thrown errors. Something likeBeta Was this translation helpful? Give feedback.
All reactions