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.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- Missing record: call
notFound()fromnext/navigationbefore rendering content;not-found.tsxrenders and the status is 404 (not a 200 with an empty page). error.tsxmust be a client component. Props:error(typeunknown: narrow withinstanceof Errorbefore readingmessage/digest) andretry()(stable in 16.3: re-fetches and re-renders the segment).reset()only clears the error state without re-fetching.error.tsxwrapsloading,not-found,pageand nested layouts, but NOT thelayout.tsxof its own segment.global-error.tsxhandles root layout errors and must render its own<html>and<body>.- Next.js 16.3
catchError(Fallback)fromnext/errorcreates component-level boundaries that do not swallownotFound()orredirect()and canretry()Server Components. - In production, Server Component errors reach the client with a generic message plus
error.digestto match server logs. redirect()andnotFound()work by throwing special errors: never call them inside atrywhosecatchswallows everything.- Expected errors in Server Actions: return
{ message }and show it withuseActionState, instead of throwing. - Error boundaries do not catch event handler errors; errors inside
startTransitiondo bubble to the nearest boundary. Report server errors withinstrumentation.tsonRequestError.
The red pen
Common traps: mistake → fix
Mistake: "Return
nullwhen 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.tsxcatches 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
- A product slug that does not exist returns 200 with an empty page. Fix it.
- Explain
error.tsx,global-error.tsx,not-found.tsxandcatchError. - Why must
error.tsxbe a client component? - What is the difference between
retry()andreset()? - 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 voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.