// app/dashboard/error.tsx
'use client'; // Must be a Client Component
import { useEffect } from 'react';
export default function Error({
error,
reset,
}: {
error: Error & { digest?: string };
reset: () => void;
}) {
useEffect(() => {
// Log error to reporting service
console.error(error);
}, [error]);
return (
<div className="error-container">
<h2>Something went wrong!</h2>
<p>{error.message}</p>
<button onClick={reset}>Try again</button>
</div>
);
}// app/global-error.tsx
'use client';
export default function GlobalError({
error,
reset,
}: {
error: Error;
reset: () => void;
}) {
return (
<html>
<body>
<h2>Something went wrong!</h2>
<button onClick={reset}>Try again</button>
</body>
</html>
);
}// app/not-found.tsx
import Link from 'next/link';
export default function NotFound() {
return (
<div>
<h2>404 - Page Not Found</h2>
<p>Could not find the requested resource</p>
<Link href="/">Return Home</Link>
</div>
);
}
// Trigger programmatically
import { notFound } from 'next/navigation';
async function Page({ params }) {
const post = await getPost(params.id);
if (!post) {
notFound(); // Shows not-found.tsx
}
return <article>{post.content}</article>;
}// app/posts/[id]/not-found.tsx
export default function PostNotFound() {
return (
<div>
<h2>Post Not Found</h2>
<p>The post you're looking for doesn't exist.</p>
</div>
);
}// app/api/users/route.ts
import { NextResponse } from 'next/server';
export async function GET() {
try {
const users = await db.user.findMany();
return NextResponse.json(users);
} catch (error) {
console.error('Database error:', error);
return NextResponse.json(
{ error: 'Internal Server Error' },
{ status: 500 }
);
}
}app/
├── global-error.tsx # Catches root layout errors
├── error.tsx # Catches app-level errors
├── dashboard/
│ ├── error.tsx # Catches dashboard errors
│ └── settings/
│ └── error.tsx # Catches settings errors
Learned: December 20, 2025 Tags: Next.js, Error Handling, Error Boundaries