Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Next.js 16+ · Directive

error

Use a route segment's error.tsx file to catch unexpected runtime failures and render fallback UI. Next.js supplies the error and a reset callback so the boundary can report the failure and retry.

How it works

The error.tsx convention provides route-scoped fallback UI for unexpected runtime failures. It must be a Client Component because the fallback can use client-side behavior such as effects and retry actions; the boundary receives the thrown error and a reset callback.

Syntax

export default function Error({ error, reset }: { error: Error & { digest?: string }; reset: () => void }) { /* fallback UI */ }

Parameters

NameTypeDescription
errorError & { digest?: string }The thrown runtime error passed to the fallback component; it may include a digest for identifying server-side failures.
reset() => voidA callback that retries rendering the contents protected by the boundary.

Returns

Fallback React UI rendered when an unexpected runtime error occurs within the protected route segment or its nested children.

Key points

  • Create error.tsx in a route segment to provide fallback UI for unexpected runtime failures.
  • The convention scopes protection to the segment and descendants beneath that folder.
  • The exported component replaces the failed content instead of requiring a manually configured React boundary.

Common mistakes

  • A boundary does not necessarily handle every descendant error if a nearer nested error.tsx catches it first.

Example: Segment error boundary

// app/dashboard/invoices/error.tsx
'use client'

import { useEffect } from 'react'

export default function Error({
  error,
  reset,
}: {
  error: Error & { digest?: string }
  reset: () => void
}) {
  useEffect(() => {
    console.error(error)
  }, [error])

  return (
    <main>
      <h2>Something went wrong!</h2>
      <button onClick={() => reset()}>Try again</button>
    </main>
  )
}

Place a client component named error.tsx in a route segment. Next.js uses it as an automatically inserted error boundary for that segment and its descendants; when rendering fails, it receives the error and a reset function so the UI can report the problem and retry.

When to use it

Segment-specific recovery

A dashboard page or nested route fails during rendering.

In-place retry

A user can retry a transient request or rendering failure.

Error reporting and fallback UI

The application needs route-scoped diagnostics and a friendly fallback.

Granular isolation

Different parts of a nested application need different recovery boundaries.

Interview questions

QWhat does error.tsx do in the App Router?

Next.js treats error.tsx as a route-segment error boundary and wraps the segment's nested children. If rendering throws within that boundary, the exported component renders as fallback UI and receives error plus reset.

Follow-up: How do you control error granularity? Add error.tsx at the route level where the desired boundary should begin; nested boundaries can isolate more specific failures.

QWhy is error.tsx a Client Component, and what is reset for?

The component must be client-side because the documented pattern uses client behavior, including a retry button and useEffect for logging. The reset callback retries rendering the failed segment.

Follow-up: What should production logging consider? The error object may be sanitized, so use the available digest to correlate the failure with server-side logs.

QHow does boundary placement affect which errors are caught?

Errors are handled by the closest applicable boundary in the route hierarchy. Putting error.tsx at different folder levels lets an application choose whether a failure replaces a small portion of a page or a larger route subtree.

Follow-up: How are root-layout failures handled? Use global-error, which can replace the root layout when errors occur there.

  • global-error
  • not-found
  • loading
  • layout
  • React Error Boundary

Source: File-system conventions: error.js - Next.js. Checked 2026-10-15.