How to handle errors gracefully in Remix
How to use ErrorBoundary and CatchBoundary in error handling
Thursday, December 16, 2021
How Remix handles errors
From the Remix official documentation
Remix will automatically catch errors and render the nearest error boundary for errors thrown while:
- rendering in the browser
- rendering on the server
- in a loader during the initial server rendered document request
- in an action during the initial server rendered document request
- in a loader during a client-side transition in the browser (Remix serializes the error and sends it over the network to the browser)
- in an action during a client-side transition in the browser
What can we use to handle errors in Remix?
Since Remix is still a JavasScript framework, we can still use JavaScript constructs to catch errors such as try/cath
.
This blog will only discuss how to catch errors using ErrorBoundary
and CatchBoundary
.
Using ErrorBoundary
From the official Docs
An ErrorBoundary is a React component that renders whenever there is an error anywhere on the route, either during rendering or during data loading.
Here is an example of using the ErrorBoundary
component to catch errors in Remix.
.tsx12345
Using the ErrorBoundary
above, Here are some examples of errors we can catch and the corresponding result.
-
Explicitly thrown errors
.tsx123
.html
-
Unhandled errors
.tsx123
.html
-
Other errors in backend
.tsx1234
.html
-
Errors in React land
.tsx12345
.html
Using CatchBoundary
A CatchBoundary is a React component that renders whenever an action or loader throws a Response.
It works in combination with useCatch
. The hook will allow us to get a reference to the response's data
, status
, and statusText
.
.tsx123456789101112
.tsx123
will yield to
.html
We can also use other data formats such as plain string,
but we need to make sure our CatchBoundary
can handle them properly.
.tsx123
will yield to
.html
In which cases should we use one over the other?
We should use CatchBoundary
if we care about the context of the error, such as status codes or the custom data related to the error; in another case, we use ErrorBoundary
.
The good thing is we can even use them together. So, for example, if there's an error in the CatchBoundary
, that error will be caught by the ErrorBoundary
.
Live examples
Handling global 404 error
Handling route 404 error
Handling error in an action
Try to fill in the form, then click submit to see the 401
error.
Handing error in a nested route
Propagating error from /test
to the root route
Additional note:
Your root
ErrorBoundary
should not throw an error in a production environment since there's nothing else to catch the error. If the rootErrorBoundary
can throw an error, the user might see something like this
Conclusion
I find using ErrorBoundary
and CatchBoundary
an easy way to handle route errors in Remix as it allows us to have fine-grained control on how we want to display errors in our pages.
In my opinion, it improves the readability of what we are trying to achieve as there is the precise placement of the success and error cases.