typescriptintermediate
Next.js Error Handling Patterns
Implement error.tsx, not-found.tsx, and global-error.tsx for comprehensive error handling.
typescriptPress ⌘/Ctrl + Shift + C to copy
// app/dashboard/error.tsx
'use client';
export default function DashboardError({
error,
reset,
}: {
error: Error & { digest?: string };
reset: () => void;
}) {
return (
<div className="flex flex-col items-center justify-center min-h-[50vh]">
<h2 className="text-2xl font-bold mb-4">Something went wrong!</h2>
<p className="text-gray-500 mb-4">{error.message}</p>
<button
onClick={reset}
className="px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600"
>
Try again
</button>
</div>
);
}
// app/global-error.tsx (catches root layout errors)
'use client';
export default function GlobalError({
error,
reset,
}: {
error: Error & { digest?: string };
reset: () => void;
}) {
return (
<html>
<body>
<h2>Something went wrong!</h2>
<button onClick={reset}>Try again</button>
</body>
</html>
);
}
// app/dashboard/not-found.tsx
import Link from 'next/link';
export default function NotFound() {
return (
<div className="text-center py-20">
<h2 className="text-3xl font-bold mb-2">Page Not Found</h2>
<p className="text-gray-500 mb-4">Could not find the requested resource.</p>
<Link href="/dashboard" className="text-blue-500 underline">
Back to Dashboard
</Link>
</div>
);
}
// Trigger not-found in a server component
import { notFound } from 'next/navigation';
export default async function Page({ params }: { params: { id: string } }) {
const item = await getItem(params.id);
if (!item) notFound();
return <div>{item.name}</div>;
}Use Cases
- Graceful error recovery in nested layouts
- Custom 404 pages per route segment
- Client-side error boundaries with retry
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
typescriptintermediate
Error Handling with error.tsx and not-found.tsx
Handle errors gracefully with error.tsx boundaries and not-found.tsx pages in App Router.
Best for: error boundaries
#nextjs#error-handling
typescriptintermediate
Error Boundary with Fallback UI
Class-based error boundary component that catches render errors and displays a customizable fallback UI.
Best for: Graceful error recovery
#error-boundary#error-handling
typescriptintermediate
React Error Boundary Component
Build a reusable error boundary with retry, fallback UI, and error reporting integration.
Best for: Preventing full-page crashes from component errors
#react#error-boundary
typescriptintermediate
Error Boundary with Fallback UI
Error boundary class component with customizable fallback UI, retry button, and error reporting.
Best for: Graceful error recovery
#error-boundary#error-handling