What is the purpose of the error.tsx file in Next.js?
The error.tsx file is used to create a custom error page that handles unexpected errors in your Next.js app, showing friendly messages instead of default error screens.
Click to reveal answer
beginner
How do you trigger an error to be caught by error.tsx in Next.js?
You can throw an error inside your components or server functions. Next.js will catch it and render the error.tsx page automatically.
Click to reveal answer
intermediate
Which React hook is commonly used inside error.tsx to log error details?
The useEffect hook is often used to log or handle side effects when an error occurs, but Next.js passes error info as props to error.tsx for display.
Click to reveal answer
beginner
What is a good user experience practice when designing an error page in error.tsx?
Show a clear message explaining something went wrong, provide a way to go back or reload, and keep the design consistent with the rest of your app.
Click to reveal answer
intermediate
Can error.tsx handle both client-side and server-side errors in Next.js?
Yes, error.tsx handles errors from both client and server, providing a unified way to show error messages across your app.
Click to reveal answer
What file name does Next.js use by default for custom error pages?
Aapp-error.tsx
B404.tsx
Ccustom-error.tsx
Derror.tsx
✗ Incorrect
Next.js uses error.tsx as the default file for custom error pages.
How does Next.js know to show your custom error page?
AWhen you call a special Next.js API
BWhen you import <code>error.tsx</code> manually
CWhen an error is thrown in your app
DWhen you refresh the page
✗ Incorrect
Next.js automatically shows the custom error page when an error is thrown anywhere in your app.
Which of these is NOT a good practice for error pages?
ADisplay raw error stack to users
BShow a friendly message
CProvide navigation options
DKeep design consistent with app
✗ Incorrect
Showing raw error stacks can confuse users and expose sensitive info; better to show friendly messages.
Can error.tsx receive error details as props?
AYes, Next.js passes error info as props
BNo, it only shows static content
COnly if you use a special hook
DOnly on client side
✗ Incorrect
Next.js passes error details as props to error.tsx so you can display or log them.
What happens if you don't create an error.tsx file?
AApp crashes without message
BNext.js shows a default error page
CErrors are ignored
DApp reloads automatically
✗ Incorrect
Without error.tsx, Next.js shows its default error page.
Explain how Next.js uses error.tsx to handle errors in your app.
Think about what happens when your app crashes.
You got /4 concepts.
Describe best practices for designing a user-friendly error page in error.tsx.
Imagine you are helping a friend who sees an error.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the error.tsx file in a Next.js route?
easy
A. To catch and display errors that happen in that route
B. To define the page layout and styles
C. To fetch data from an API
D. To handle user authentication
Solution
Step 1: Understand the role of error.tsx
This file is designed to catch errors that occur in its route and show a friendly message to users.
Step 2: Compare with other options
Options A, C, and D describe unrelated tasks like layout, data fetching, or authentication, which are not the purpose of error.tsx.
Final Answer:
To catch and display errors that happen in that route -> Option A
Quick Check:
Error handling = To catch and display errors that happen in that route [OK]
Hint: Remember: error.tsx shows errors in its route [OK]
Common Mistakes:
Confusing error.tsx with layout.tsx
Thinking error.tsx fetches data
Assuming error.tsx manages authentication
2. Which of the following is the correct way to export an error component in error.tsx for Next.js?
easy
A. const Error = () => {
Error occurred
}; export default Error;
B. function Error() { return
Error occurred
; }
C. export default function Error() { return
Error occurred
; }
D. export function Error() { return
Error occurred
; }
Solution
Step 1: Check correct export syntax
Next.js expects a default export for the error component in error.tsx. export default function Error() { return
Error occurred
; } uses export default function, which is correct.
Step 2: Identify syntax errors in other options
function Error() { return
Error occurred
; } lacks export, so it won't be recognized. export function Error() { return
Error occurred
; } exports a named function, not default. const Error = () => {
Error occurred
}; export default Error; misses a return statement inside the arrow function, causing no output.
Final Answer:
export default function Error() { return <p>Error occurred</p>; } -> Option C
Quick Check:
Default export function = export default function Error() { return
Error occurred
; } [OK]
Hint: Use default export function for error component [OK]
Common Mistakes:
Forgetting to export default
Using named exports instead of default
Missing return statement in arrow function
3. Given this error.tsx component, what will be rendered when an error occurs?
D. A section with a heading and a button that reloads the page
Solution
Step 1: Analyze the JSX structure
The component returns a <section> with role="alert", containing a heading and a button with an onClick that reloads the page.
Step 2: Understand the rendered output
When an error happens, this UI shows a message and a clickable button that refreshes the page, matching A section with a heading and a button that reloads the page.
Final Answer:
A section with a heading and a button that reloads the page -> Option D
Quick Check:
JSX returns alert section with reload button = A section with a heading and a button that reloads the page [OK]
Hint: Look for JSX elements and event handlers to predict output [OK]
Common Mistakes:
Ignoring the button's onClick behavior
Assuming no UI renders on error
Confusing role attribute with content
4. You wrote this error.tsx but it does not show the error UI when an error happens:
function Error() {
return <p>Error!</p>;
}
export default Error;
What is the likely problem?
medium
A. The component name must be lowercase
B. The file must be named error.tsx and placed in the route folder
C. The component is not exported as default
D. The component must return a div, not a p
Solution
Step 1: Check export and component name
The component is exported as default and named with uppercase, which is correct.
Step 2: Verify file placement and naming
Next.js requires the error.tsx file to be in the route folder to catch errors there. If misplaced or misnamed, it won't work.
Final Answer:
The file must be named <code>error.tsx</code> and placed in the route folder -> Option B
Quick Check:
Correct file name and location = The file must be named error.tsx and placed in the route folder [OK]
Hint: Check file name and folder location for error.tsx [OK]
Common Mistakes:
Using lowercase component name
Returning wrong HTML tags
Not placing error.tsx in the route folder
5. You want to improve accessibility in your error.tsx component. Which change best helps screen reader users?
hard
A. Add role="alert" to the container element
B. Use only <div> elements without roles
C. Remove all buttons to avoid confusion
D. Use inline styles to highlight the error text
Solution
Step 1: Understand accessibility roles
Adding role="alert" tells screen readers to announce the error immediately, improving accessibility.
Step 2: Evaluate other options
Use only <div> elements without roles lacks roles, reducing accessibility. Remove all buttons to avoid confusion removes helpful retry buttons. Use inline styles to highlight the error text styles visually but doesn't help screen readers.
Final Answer:
Add <code>role="alert"</code> to the container element -> Option A
Quick Check:
Use ARIA roles for accessibility = Add role="alert" to the container element [OK]
Hint: Use ARIA roles like role="alert" for error messages [OK]
Common Mistakes:
Ignoring ARIA roles for screen readers
Removing interactive elements unnecessarily
Relying only on visual styles for errors
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.