Error handling with error.tsx helps you show friendly messages when something goes wrong in your Next.js app. It keeps your app from crashing and helps users understand the problem.
Error handling with error.tsx in NextJS
'use client'; export default function Error({ error, reset }) { return ( <main> <h1>Something went wrong!</h1> <p>{error.message}</p> <button onClick={() => reset()}>Try again</button> </main> ); }
The Error component receives two props: error (the error object) and reset (a function to retry rendering).
Place error.tsx inside the folder of the route you want to handle errors for.
'use client'; export default function Error({ error, reset }) { return ( <div> <h2>Error occurred:</h2> <pre>{error.message}</pre> <button onClick={() => reset()}>Reload</button> </div> ); }
role="alert" and aria-label on the button.'use client'; export default function Error({ error, reset }) { return ( <section role="alert"> <h1>Oops!</h1> <p>We hit a problem: {error.message}</p> <button onClick={() => reset()} aria-label="Try again">Try again</button> </section> ); }
This example shows a page that throws an error when you click the button. The error.tsx file catches the error and shows a friendly message with a button to try again.
import { useState } from 'react'; 'use client'; export default function Page() { const [throwError, setThrowError] = useState(false); if (throwError) { throw new Error('This is a test error'); } return ( <main> <h1>Welcome to the page</h1> <button onClick={() => setThrowError(true)}>Cause Error</button> </main> ); } // error.tsx 'use client'; export default function Error({ error, reset }) { return ( <main role="alert"> <h1>Oops! Something went wrong.</h1> <p>{error.message}</p> <button onClick={() => reset()} aria-label="Try again">Try again</button> </main> ); }
Always provide a way to reset or retry after an error to improve user experience.
Use semantic HTML and ARIA roles for accessibility in your error UI.
Place error.tsx in the route folder where you want to handle errors specifically.
Error handling with error.tsx helps catch and show errors in Next.js routes.
It improves user experience by showing friendly messages and retry options.
Use accessible HTML and place the file in the right route folder.
Practice
error.tsx file in a Next.js route?Solution
Step 1: Understand the role of
This file is designed to catch errors that occur in its route and show a friendly message to users.error.tsxStep 2: Compare with other options
Options A, C, and D describe unrelated tasks like layout, data fetching, or authentication, which are not the purpose oferror.tsx.Final Answer:
To catch and display errors that happen in that route -> Option AQuick Check:
Error handling = To catch and display errors that happen in that route [OK]
- Confusing error.tsx with layout.tsx
- Thinking error.tsx fetches data
- Assuming error.tsx manages authentication
error.tsx for Next.js?Solution
Step 1: Check correct export syntax
Next.js expects a default export for the error component inerror.tsx. export default function Error() { returnError occurred
; } usesexport default function, which is correct.Step 2: Identify syntax errors in other options
function Error() { returnError occurred
; } lacks export, so it won't be recognized. export function Error() { returnError 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 CQuick Check:
Default export function = export default function Error() { returnError occurred
; } [OK]
- Forgetting to export default
- Using named exports instead of default
- Missing return statement in arrow function
error.tsx component, what will be rendered when an error occurs?export default function Error() {
return (
<section role="alert">
<h1>Oops! Something went wrong.</h1>
<button onClick={() => window.location.reload()}>Try again</button>
</section>
);
}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 DQuick Check:
JSX returns alert section with reload button = A section with a heading and a button that reloads the page [OK]
- Ignoring the button's onClick behavior
- Assuming no UI renders on error
- Confusing role attribute with content
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?
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 theerror.tsxfile 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 BQuick Check:
Correct file name and location = The file must be namederror.tsxand placed in the route folder [OK]
- Using lowercase component name
- Returning wrong HTML tags
- Not placing error.tsx in the route folder
error.tsx component. Which change best helps screen reader users?Solution
Step 1: Understand accessibility roles
Addingrole="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 AQuick Check:
Use ARIA roles for accessibility = Addrole="alert"to the container element [OK]
- Ignoring ARIA roles for screen readers
- Removing interactive elements unnecessarily
- Relying only on visual styles for errors
Ready to prove it?
Pass the test and this pattern moves unknown
