Skip to solution
mediumFrontend

What is error.js in Next.js and how does error handling work in the App Router?

733 views
01

Understand the problem

Segment-level error boundaries with reset.

nextjserror-handlingerror-boundaryapp-router
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

error.js defines a Client Component error boundary for its segment, catching render errors and showing fallback UI with a reset() to retry. global-error.js catches root-layout errors. Expected errors are handled in code; error.js is for unexpected ones.

Solution ready — 2 min read

Classified // press E to declassify

04

Read the code

A segment error boundary
// app/dashboard/error.tsx
'use client';                          // error boundaries are Client Components
export default function Error({ error, reset }: { error: Error; reset: () => void }) {
  return (
    <div>
      <p>Something went wrong: {error.message}</p>
      <button onClick={reset}>Try again</button>
    </div>
  );
}
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 40 of 95 decoded in the Next.js track. One more won't hurt.

Back to track