Error boundaries
What you need to know
An error.tsx file defines the fallback shown when an unexpected rendering failure reaches the error boundary of that route segment. Keep navigation available and offer a reasonable recovery action. A missing book is an expected outcome handled differently from a broken rendering operation.
By the end, you can: design a recoverable segment error; explain reset limitations.
app/failure-demo/error.tsx
"use client";export default function ErrorView({ reset }: { reset: () => void }) { return <section><h2>Catalog is temporarily unavailable</h2> <button type="button" onClick={reset}>Try rendering again</button> </section>;}Expected behavior (not framework-tested): The fallback offers a user-triggered recovery attempt without printing an exception. This reset example attempts another render. It is not a guaranteed refetch; verify the exact error-file contract in the selected patch.
How it works
An error boundary surrounds its segment page and nested content, not the layout or template wrapping it in that same segment. A parent boundary can cover failures in a child layout. The error file must be a Client Component. The documented reset clears the boundary failure and attempts another render without refetching its contents. The error-file version history records unstable_retry in 16.2.0 and stable retry in 16.3.0; the latter also attempts to fetch the boundary content again. Use retry only in a verified compatible lane. Root layout or template failures require a separate global-error file that supplies its own html and body. Neither promises success if the underlying problem persists. Expected validation failures should be modeled as ordinary data or messages, not deliberately thrown into a generic fallback.
app/failure-demo/page.tsx
export const dynamic = "force-dynamic";export default async function Page({ searchParams }: { searchParams: Promise<{ fail?: string }> }) { const { fail } = await searchParams; if (fail === "1") throw new Error("Synthetic catalog failure"); return <h1>Catalog available</h1>;}Expected behavior (not framework-tested): In an isolated teaching fixture, ?fail=1 should reach the segment error boundary. Retrying without changing that condition still fails; visiting the route without the query allows a fresh successful rendering.
Common mistake
Do not put raw error.message, stack traces or request data into production fallback UI. Do not retry endlessly or use an error boundary as a replacement for validation, event-handler error handling or authorization.
Practice and verification
Place a segment error boundary around the demo, test fail=1 and normal load, then navigate to another page. Compare a retry with a corrected URL. Record whether the selected version provides retry and reset.
Scope: App Router, proposed Next.js 16.x, Cache Components disabled for these labs. Snippets assume a reviewed TypeScript app and are static examples; framework behavior has not been executed. Exact supported patch and dependency set must be verified before running.
Further reading: Error · Error Guide