Skip to the page

App Router foundations · mid

Errors and not-found in the App Router

Model expected errors as return values, call notFound() for missing records so the response is a real 404, and let error.tsx (route level), catchError (component level, 16.3) and global-error.tsx (root layout) catch unexpected errors with a retry() that re-fetches server content.

The notes

What you must know

  • Missing record: call notFound() from next/navigation before rendering content; not-found.tsx renders and the status is 404 (not a 200 with an empty page).
  • error.tsx must be a client component. Props: error (type unknown: narrow with instanceof Error before reading message/digest) and retry() (stable in 16.3: re-fetches and re-renders the segment). reset() only clears the error state without re-fetching.
  • error.tsx wraps loading, not-found, page and nested layouts, but NOT the layout.tsx of its own segment. global-error.tsx handles root layout errors and must render its own <html> and <body>.
  • Next.js 16.3 catchError(Fallback) from next/error creates component-level boundaries that do not swallow notFound() or redirect() and can retry() Server Components.
  • In production, Server Component errors reach the client with a generic message plus error.digest to match server logs.
  • redirect() and notFound() work by throwing special errors: never call them inside a try whose catch swallows everything.
  • Expected errors in Server Actions: return { message } and show it with useActionState, instead of throwing.
  • Error boundaries do not catch event handler errors; errors inside startTransition do bubble to the nearest boundary. Report server errors with instrumentation.ts onRequestError.

The red pen

Common traps: mistake → fix

  • Mistake: "Return null when the product is missing"

    Fix: sends a 200 with an empty page; call notFound().

  • Mistake: "Wrap redirect() in try/catch"

    Fix: the catch swallows the redirect.

  • Mistake: "error.tsx catches errors in the layout next to it"

    Fix: it does not; use the parent segment or global-error.

Easy to hard

Questions interviewers ask, easy to hard

  1. A product slug that does not exist returns 200 with an empty page. Fix it.
  2. Explain error.tsx, global-error.tsx, not-found.tsx and catchError.
  3. Why must error.tsx be a client component?
  4. What is the difference between retry() and reset()?
  5. How do you report server errors to Sentry or OpenTelemetry?

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview