Error Boundaries
Learn how to handle errors gracefully with error boundaries in Bini.js.
What are Error Boundaries?
Error boundaries are React components that catch JavaScript errors in their child component tree, log those errors, and display a fallback UI instead of the component tree that crashed. In Bini.js, you can create error boundaries using the error.tsx file.
Error boundaries catch errors during rendering, in lifecycle methods, and in constructors of the whole tree below them.
Creating an Error Boundary
Create an error.tsx file in any folder to define an error boundary for that route and its children.
Error Props
The error.tsx component receives two props:
| Prop | Type | Description |
|---|---|---|
| error | Error | The thrown Error object with message and stack trace |
| reset | () => void | A function that clears the error state and re-renders children |
Nested Error Boundaries
You can create nested error boundaries by placing error.tsx in subdirectories. Each error boundary only catches errors in its subtree.
| Route | Error Boundary Used |
|---|---|
| /blog/hello-world | app/blog/error.tsx |
| /dashboard | app/dashboard/error.tsx |
| /dashboard/settings | app/dashboard/settings/error.tsx |
| /about | app/error.tsx (global) |
Nearest Wins Resolution
Error boundaries use "nearest wins" resolution. The closest error.tsx to the route where the error occurred is used.
When an error occurs:
- Check the route's own folder for
error.tsx - If not found, check each parent folder (going up)
- If still not found, use the built-in fallback
Error with Layout
Error boundaries are rendered inside the layout hierarchy. Layouts remain visible when an error occurs in a child route.
This allows you to keep navigation, headers, and sidebars visible even when an error occurs in the main content area.
Built-in Fallback
If no error.tsx exists in the hierarchy, Bini.js uses a built-in fallback:
- Development: Renders
nullsobini-overlaytakes over with an animated error badge and full error panel - Production: Shows a generic "Something went wrong" UI with a "Try again" button
- Error logging: Errors are dispatched as a
__bini_error__CustomEvent on window for external dev overlays
Creating custom error boundaries is recommended for production applications to provide a better user experience.