catchError
The catchError function creates a component that wraps its children in an error boundary. It provides a programmatic alternative to the error.js file convention, enabling component-level error recovery anywhere in your component tree.
Compared to a custom React error boundary, catchError is designed to work with Next.js out of the box:
- Built-in error recovery —
retry()re-renders the page inside a Transition, preserving Client Components state outside of the error boundary. - Framework-aware integration — APIs like
redirect()andnotFound()work by throwing special errors under the hood.catchErrorhandles these seamlessly, so they're not accidentally caught by your error boundary. - Client navigation handling — The error state automatically clears when you do a client navigation to a different route.
catchError can be called from Client Components.
Reference
Parameters
catchError accepts a single argument:
fallback
A function that renders the error UI when an error is caught. It receives two arguments:
props— The props passed to the wrapper component (excludingchildren).errorInfo— An object containing information about the error:
| Property | Type | Description |
|---|---|---|
error | unknown | The value that was thrown. Narrow it, for example with instanceof Error, before reading properties like message. |
retry | () => void | Re-fetches and re-renders the error boundary's children. If successful, the fallback is replaced with the re-rendered result. |
reset | () => void | Resets the error state and re-renders without re-fetching. Use retry() in most cases. |
The fallback function must be a Client Component (or defined in a 'use client' module).
Returns
catchError returns a React component that:
- Accepts the same props as your fallback's first argument, plus
children. - Wraps
childrenin an error boundary. - Renders the
fallbackwhen an error is caught inchildren.
Examples
Basic usage
Define a fallback and use the returned component to wrap parts of your UI:
Recovering from errors
Use retry() to prompt the user to recover from the error. When called, the function re-fetches and re-renders the error boundary's children. If successful, the fallback is replaced with the re-rendered result.
In most cases, use retry() instead of reset(). The reset() function only clears the error state and re-renders without re-fetching, which means it won't recover from Server Component errors.
Server-rendered error fallback
You can pass server-rendered content as a prop to display data-driven fallback UI. This works by rendering a Server Component as a React.ReactNode prop that the fallback displays when an error is caught.
Good to know
This pattern eagerly renders the fallback on every page render, even when no error occurs. For most use cases, a simpler client-side fallback is sufficient.
Good to know
- Unlike the
error.jsfile convention which is scoped to route segments,catchErrorcan be used to wrap any part of your component tree for component-level error recovery. - Props passed to the wrapper component are forwarded to the fallback function, making it easy to create reusable error UIs with different configurations.
- You don't need to wrap
error.jsdefault exports withcatchError. Theerror.jsfile convention already renders inside a built-in error boundary provided by Next.js.
Version History
| Version | Changes |
|---|---|
v16.3.0 | catchError became stable. |
v16.2.0 | unstable_catchError introduced. |
Learn more about error handling
Error Handling
error.js
Last updated