Question presented to candidate: "A Server Component on our orders page sometimes throws because an upstream API is flaky. What does the user see by default, how do you show a friendly error with a retry button, and what ends up in the browser versus the server logs?"
What a strong answer should cover:
error.tsxwraps its segment (page, loading, not-found and nested layouts) in a React error boundary and renders as the fallback; it must be a Client Component.- It does not catch errors from the
layout.tsxortemplate.tsxof its own segment; those bubble to the parent boundary, and the root layout needsglobal-error.tsxwith its own html and body. - Props in 16.x:
error(typedunknown; narrow before reading),retry()which re-fetches and re-renders the segment (stable since 16.3), andreset()which only re-renders. - In production, Server Component errors reach the browser with a generic message and a
digestthat matches the full error in the server log; Client Component errors keep their message. - Event handler and async errors outside rendering are not caught by error boundaries; handle them with try/catch or state.
- Expected failures (missing record, no access) use
notFound(),forbidden()or returned values, not thrown errors;catchErrorgives component-level boundaries.
Clarifying questions expected:
- "Should the navigation stay usable when the content fails?" β decides which segment gets the
error.tsx. - "Is the failure expected or a bug?" β expected cases should be modelled as data or a 404, not as an error.
Code / implementation expected: Yes β a segment error.tsx using retry() and the digest, a catchError boundary for one widget, and global-error.tsx.