Comprehensive Error Handling
Front-Commerce provides a robust error handling system that allows you to gracefully manage errors at different levels of your application. This guide will walk you through the process of implementing error boundaries and customizing error pages to enhance your application's resilience and user experience.
Understanding Error Boundaries
Error boundaries in Front-Commerce are based on Remix's error handling mechanism. They allow you to catch JavaScript errors anywhere in their child component tree, log those errors, and display a fallback UI instead of the component tree that crashed.
Front-Commerce offers three main types of error boundaries:
- Root Error Boundary
- Layout Error Boundary
- Route-specific Error Boundary
Root Error Boundary
The Root Error Boundary is the top-level error handler for your entire
application. It's typically defined in your app/root.tsx
file.
To implement the Root Error Boundary:
-
Import the
RootErrorBoundary
component from Front-Commerce's theme:import { RootErrorBoundary } from "theme/pages/Error";
-
Add the ErrorBoundary export to your
root.tsx
:export const ErrorBoundary = RootErrorBoundary;
This will catch any unhandled errors in your application and display a generic error page.
The RootErrorBoundary
is wrapped in the SimpleLayout
component.
Layout Error Boundary
For routes that use a custom layout, you can implement a Layout Error Boundary. This allows you to maintain your layout structure even when an error occurs.
Here is an example of how we use the LayoutErrorBoundary for the _main.tsx
layout:
-
Import the
LayoutErrorBoundary
component:app/routes/_main.tsximport { LayoutErrorBoundary } from "theme/pages/Error";
-
Use it within your layout component's ErrorBoundary:
app/routes/_main.tsxexport function ErrorBoundary() {
// If this loader fails, it will be caught by the RootErrorBoundary
const data = useRouteLoaderData<typeof loader>("routes/_main");
const { headerNavigationMenu = [], footerNavigationMenu = [] } =
data || {};
return (
<Layout
headerNavigationMenu={headerNavigationMenu}
footerNavigationMenu={footerNavigationMenu}
>
<LayoutErrorBoundary />
</Layout>
);
}
This approach ensures that your layout remains intact while displaying the error message.
The LayoutErrorBoundary
is not wrapped in any layout as this is meant to be
used inside a route which is already wrapped in a layout.
Customizing Error Pages
Front-Commerce allows you to customize error pages for specific HTTP status
codes. This is done through the RouteResponseError
component and the
appErrorPages
object.
Adding Custom Error Pages
-
Create a new component for your custom error page, e.g.,
CustomNotFound.tsx
. -
Add your custom error page to the
appErrorPages
object intheme/pages/Error/appErrorPages.ts
:theme/pages/Error/appErrorPages.tsimport CustomNotFound from "./CustomNotFound";
export const appErrorPages = {
404: CustomNotFound,
} satisfies AppErrorPages; -
The
RouteResponseError
component will now use your custom component for 404 errors.
Default Error Pages
Front-Commerce provides default error pages for common status codes:
You can override these by adding your own components to the
theme/pages/Error/appErrorPages.ts
object.
Best Practices
- Granular Error Handling: Use route-specific error boundaries for handling errors that are unique to certain parts of your application.
- Informative Error Messages: Provide clear and helpful error messages to guide users on what went wrong and what they can do next.
- Logging: Implement proper error logging to help with debugging and monitoring application health.
- Graceful Degradation: Design your error pages to maintain as much functionality as possible, allowing users to navigate away from the error or retry their action.
- Consistency: Maintain a consistent look and feel in your error pages to align with your application's overall design.
By following these guidelines and utilizing Front-Commerce's error handling components, you can create a robust and user-friendly error management system for your e-commerce application.