# The error.js Convention

> Gracefully isolate crashes without bringing down the whole app.

Subject: Next.js | Lesson 10 of 65 | 5 min read
Interactive lesson: https://codewithanimation.in/nextjs/error-file
Previous: [The loading.js Convention](https://codewithanimation.in/nextjs/loading-file.md) | Next: [The global-error.js Convention](https://codewithanimation.in/nextjs/global-error-file.md)

## Graceful degradation

In any application, things will eventually fail. A database query might time out, or an API might return malformed data. When a React component throws an unhandled error, it typically crashes the entire webpage.

The `error.js` (or `.tsx`) file convention prevents this. It acts as a safety net, automatically displaying a custom fallback UI when something goes wrong inside its route segment.

## Isolating the blast radius

The primary goal of `error.js` is isolation. If the dashboard analytics page crashes, the user shouldn't be met with a blank white screen.

Instead, the specific analytics section should show an error message, while the surrounding `layout.js` (the sidebar, the navigation, the header) remains completely functional, allowing the user to click away and recover.

## The mandatory 'use client'

To use it, export a React component from an `error.tsx` file. Next.js will automatically wrap the route in a React `<ErrorBoundary>`.

Crucially, this file **MUST** include the `'use client'` directive at the top. React Error Boundaries rely on client-side lifecycle methods to catch rendering errors, so they cannot be Server Components.

## Common mistake: The sibling layout blindspot

A major source of confusion is exactly *what* the error file catches.

Because Next.js nests the component tree in a specific order, an `error.js` boundary is placed *inside* the `layout.js` of the same folder. This means `error.js` will catch errors in `page.js`, but it will **not** catch errors thrown by its sibling `layout.js`.

## Tip: The reset function

Your `error.js` component automatically receives two props: `error` (the actual error object) and `reset`.

`reset` is a function that you can attach to a button (e.g., 'Try again'). When clicked, it attempts to re-render the Error Boundary's contents, giving the user a chance to recover from a transient network glitch.

## Try it: Isolating the Blast Radius

Click the button to force the Page to crash. Watch how the Error Boundary catches it, replacing only the inner content while the Layout survives untouched.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/nextjs/error-file

## Check yourself

### Question 1

Why must an error.js file include the 'use client' directive?

- A. Because it needs to read from localStorage
- B. Because React Error Boundaries rely on client-side component lifecycle methods
- C. Because Server Components never throw errors
- D. Because errors can only happen in the browser

Correct answer: B

Under the hood, `error.js` creates a React `<ErrorBoundary>`. These boundaries require client-side execution to intercept and handle rendering failures.

- Why not A: While true for some client components, this isn't why error boundaries require it.
- Why not C: Server Components throw errors constantly (e.g., failed DB connections).
- Why not D: Errors can happen on both server and client.

### Question 2

If you place an error.js and a layout.js in the EXACT SAME folder, will the error.js catch errors thrown by that layout.js?

- A. Yes, it catches all errors in the folder
- B. No, because the layout wraps outside the error boundary
- C. Yes, but only if the error happens on the client
- D. No, layouts are completely immune to errors

Correct answer: B

Next.js renders the component tree like this: `<Layout><ErrorBoundary><Page /></ErrorBoundary></Layout>`. Because the layout is on the outside, an error inside the layout will bypass this boundary and bubble up to the parent folder.

- Why not A: The nesting order prevents this.
- Why not C: The environment doesn't change the nesting order.
- Why not D: Layouts can definitely crash.

## Remember this

- `error.js` isolates crashes to a specific route segment.
- It MUST be a Client Component (`'use client'`).
- It provides an `error` object and a `reset()` recovery function as props.
- It does NOT catch errors in the `layout.js` of the exact same folder.
