Error Handling & Not Found
CONCEPTS:Next.js Error Handling
Question Variations
- "How do you handle errors in the Next.js App Router?"
- "What is the purpose of the `error.js` file?"
- "How do you show a 404 page for a dynamic route when the data is missing?"
- "What is the difference between `error.js` and `global-error.js`?"
Why This Is Asked
Gracefully handling errors and 404s is essential for a professional user experience. Interviewers want to see if you understand how to use Next.js’s file-based conventions to isolate errors and provide clear feedback to the user.
Key Concepts
- Error Boundaries: How
error.jswraps its segment. - Recovering from Errors: Using the
reset()function provided toerror.js. - Global Errors: Catching errors in the root layout.
notFound(): Manually triggering a 404 UI for invalid data (e.g., a non-existent product ID).
Question Variations
- “How do you handle errors in the Next.js App Router?”
- “What is the purpose of the
error.jsfile?” - “How do you show a 404 page for a dynamic route when the data is missing?”
- “What is the difference between
error.jsandglobal-error.js?”