useRouteError
Accesses the error thrown during anaction, loader, or component render to be used in a route module ErrorBoundary.
This hook only works in Data and Framework modes.
Signature
Parameters
None.Returns
unknown
The error that was thrown during route loading, action execution, or rendering. Can be any value - Error objects, Response objects, or any thrown value.
Usage
Basic error boundary
Handle Response errors
Different error types
Custom error types
JSON error data
Common Patterns
404 Not Found
Authentication errors
Development vs production errors
Logging errors
Retry mechanism
Nested error boundaries
User-friendly error messages
Type Safety
Type guards
Custom error interface
Important Notes
Error propagation
If anErrorBoundary throws or re-throws an error, it propagates to the parent route’s ErrorBoundary.
Root error boundary
Always define anErrorBoundary in your root route to catch all unhandled errors.
Render errors vs loader/action errors
The hook catches errors from:- Route
loaderfunctions - Route
actionfunctions - Component rendering (including child components)
Related
ErrorBoundary- Route error boundary exportisRouteErrorResponse- Check if error is a Responseloader- Define route loaderaction- Define route action