Global errors and diagnostics
What you need to know
Global error UI is a last-resort recovery surface for failures at the root layout or template. Because the normal root may be broken, the fallback supplies its own document structure. Keep it small and independent of the components that might have failed.
By the end, you can: choose error scope; design sanitized logging.
app/global-error.tsx
"use client";export default function GlobalError({ reset, retry }: { reset: () => void; retry?: () => void }) { return <html lang="en"><body><main><h1>River Books is unavailable</h1> <button type="button" onClick={() => retry ? retry() : reset()}>Try again</button> <p><a href="/">Return home</a></p></main></body></html>;}Expected behavior (not framework-tested): If the global boundary activates, it provides its own html and body and a minimal recovery path. It does not depend on the regular root navigation.
How it works
Choose the smallest boundary that can recover from a failure. A catalog widget should not take down unrelated pages. In production, errors originating from Server Components may arrive at a client boundary with a generic message and a digest that helps correlate server diagnostics. The digest is optional and is not a secret token or stable error classification. Development diagnostics may expose more detail; evaluate the production behavior independently. Keep any operational logging on the appropriate trusted side and redact sensitive fields.
Sanitized diagnostic projection (pure TypeScript helper)
export function diagnostic(code: "CATALOG_UNAVAILABLE" | "ROOT_FAILURE", requestId: string) { return { code, requestId };}// Example: diagnostic("ROOT_FAILURE", "lab-42")Expected behavior (not framework-tested): The synthetic event contains only an allowlisted code and a lab-generated correlation ID. A real logging adapter and safe request ID policy remain separate work.
Common mistake
Do not import the failing root layout into global-error or assume its styles are still loaded. Do not print full exception objects, cookies, headers or user-provided data as fallback messages.
Practice and verification
Design a failure matrix for a page, nested layout and root layout. Choose a fallback and a diagnostic code for each. In a separately approved production fixture, verify root error UI and ensure synthetic internal markers do not reach browser content.
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