🎓 A1Learner
🔍
0 of 50 lessons completed

Next.js error.tsx

error.tsx catches a page that throws. It shows a fallback instead of a blank screen.

The error page

The file starts with "use client". It shows the error message and a button that tries the page again.

"use client";

export default function Error({ error, retry }: { error: Error; retry: () => void }) {
  return (
    <div>
      <h1>Something went wrong!</h1>
      <p>{error.message}</p>
      <button onClick={retry}>Try again</button>
    </div>
  );
}

The plain page is fine

The check asks for /broken with no query. It answers 200.

curl -s -o /dev/null -w "HTTP %{http_code}\n" http://localhost:3000/broken

The failing page answers 500

The check asks for /broken with ?fail=1. It answers 500.

curl -s -o /dev/null -w "HTTP %{http_code}\n" "http://localhost:3000/broken?fail=1"

Example

console.log("/broken → 200, The shelf is fine");
console.log("/broken?fail=1 → 500, digest 3173634218");
console.log("Error: The bookshelf fell over");

Key points

  • Error boundary: a wrapper that catches an error in the page inside it
  • error.tsx sits next to page.tsx. It catches errors in that page only.
  • /broken returns 200 and shows "The shelf is fine". The page throws only when ?fail=1 is in the address.
  • /broken?fail=1 returns 500. The server log names the error "The bookshelf fell over" and its digest 3173634218.
  • The fallback text "Something went wrong!" sits in the compiled page chunk. It shows in the browser after hydration, so curl cannot show it.
  • A page that always throws breaks the build. The demo throws only when ?fail=1 is present.

In this example, the same page answers two ways. With no query it returns 200 and shows "The shelf is fine". With ?fail=1 it throws, returns 500, and the server log names the error and its digest. The fallback text sits in the compiled chunk and shows in the browser after hydration. A page that always throws would break the build, so the demo throws only on ?fail=1.

Note

error.tsx must start with the line "use client". That line marks the file as a Client Component, which runs in the browser. The next section explains Client Components in full.

Try it Yourself

Edit the code, then press Run.

Running is simulated · TypeScript Playground

Exercise

In Next.js 16.4.6, what is the recovery prop called in error.tsx?