Implement comprehensive error handling

- Add try-catch blocks to all server actions with retry logic
- Implement exponential backoff retry mechanism for database operations
- Add input validation to all server actions
- Create ErrorBoundary component for React error handling
- Add page-level error handlers (error.tsx, global-error.tsx)
- Add route-specific error pages for list and item routes
- Enhance database connection with graceful error handling
- Add user-friendly error messages throughout the application
- Implement proper error logging for debugging

Task: 16. Implement error handling
Requirements: All (error handling is cross-cutting)
This commit is contained in:
2025-12-01 21:18:57 +00:00
parent e51e88fddd
commit 12ce70e495
9 changed files with 796 additions and 225 deletions

46
app/error.tsx Normal file
View File

@@ -0,0 +1,46 @@
'use client'
import { useEffect } from 'react'
import { AlertTriangle } from 'lucide-react'
export default function Error({
error,
reset,
}: {
error: Error & { digest?: string }
reset: () => void
}) {
useEffect(() => {
console.error('Page error:', error)
}, [error])
return (
<div className="min-h-screen flex items-center justify-center p-4 bg-gray-50">
<div className="max-w-md w-full bg-white rounded-lg shadow-lg p-6 sm:p-8">
<div className="flex items-center justify-center w-12 h-12 sm:w-16 sm:h-16 bg-red-100 rounded-full mx-auto mb-4">
<AlertTriangle className="w-6 h-6 sm:w-8 sm:h-8 text-red-600" />
</div>
<h2 className="text-xl sm:text-2xl font-bold text-gray-900 text-center mb-2">
Something went wrong
</h2>
<p className="text-sm sm:text-base text-gray-600 text-center mb-6">
{error.message || 'We encountered an unexpected error. Please try again.'}
</p>
<div className="flex flex-col sm:flex-row gap-3">
<button
onClick={reset}
className="flex-1 px-4 py-3 bg-blue-500 hover:bg-blue-600 active:bg-blue-700 text-white rounded-lg transition-colors font-medium text-sm sm:text-base tap-target"
>
Try Again
</button>
<button
onClick={() => window.location.href = '/'}
className="flex-1 px-4 py-3 bg-gray-200 hover:bg-gray-300 active:bg-gray-400 text-gray-700 rounded-lg transition-colors font-medium text-sm sm:text-base tap-target"
>
Go Home
</button>
</div>
</div>
</div>
)
}

50
app/global-error.tsx Normal file
View File

@@ -0,0 +1,50 @@
'use client'
import { useEffect } from 'react'
import { AlertTriangle } from 'lucide-react'
export default function GlobalError({
error,
reset,
}: {
error: Error & { digest?: string }
reset: () => void
}) {
useEffect(() => {
console.error('Global error:', error)
}, [error])
return (
<html lang="en">
<body className="bg-gray-50 min-h-screen">
<div className="min-h-screen flex items-center justify-center p-4">
<div className="max-w-md w-full bg-white rounded-lg shadow-lg p-6 sm:p-8">
<div className="flex items-center justify-center w-12 h-12 sm:w-16 sm:h-16 bg-red-100 rounded-full mx-auto mb-4">
<AlertTriangle className="w-6 h-6 sm:w-8 sm:h-8 text-red-600" />
</div>
<h2 className="text-xl sm:text-2xl font-bold text-gray-900 text-center mb-2">
Application Error
</h2>
<p className="text-sm sm:text-base text-gray-600 text-center mb-6">
A critical error occurred. Please refresh the page or contact support if the problem persists.
</p>
<div className="flex flex-col sm:flex-row gap-3">
<button
onClick={reset}
className="flex-1 px-4 py-3 bg-blue-500 hover:bg-blue-600 active:bg-blue-700 text-white rounded-lg transition-colors font-medium text-sm sm:text-base"
>
Try Again
</button>
<button
onClick={() => window.location.href = '/'}
className="flex-1 px-4 py-3 bg-gray-200 hover:bg-gray-300 active:bg-gray-400 text-gray-700 rounded-lg transition-colors font-medium text-sm sm:text-base"
>
Go Home
</button>
</div>
</div>
</div>
</body>
</html>
)
}

View File

@@ -3,6 +3,7 @@ import './globals.css'
import ServiceWorkerRegistration from '@/components/ServiceWorkerRegistration'
import OfflineIndicator from '@/components/OfflineIndicator'
import { ToastProvider } from '@/components/ToastContainer'
import ErrorBoundary from '@/components/ErrorBoundary'
export const metadata: Metadata = {
title: 'Ultimate Lists',
@@ -68,11 +69,13 @@ export default function RootLayout({
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
</head>
<body className="bg-gray-50 min-h-screen">
<ToastProvider>
<ServiceWorkerRegistration />
<OfflineIndicator />
{children}
</ToastProvider>
<ErrorBoundary>
<ToastProvider>
<ServiceWorkerRegistration />
<OfflineIndicator />
{children}
</ToastProvider>
</ErrorBoundary>
</body>
</html>
)

53
app/list/[id]/error.tsx Normal file
View File

@@ -0,0 +1,53 @@
'use client'
import { useEffect } from 'react'
import { AlertTriangle } from 'lucide-react'
import Link from 'next/link'
export default function ListError({
error,
reset,
}: {
error: Error & { digest?: string }
reset: () => void
}) {
useEffect(() => {
console.error('List page error:', error)
}, [error])
const isNotFound = error.message.includes('not found')
return (
<div className="min-h-screen flex items-center justify-center p-4 bg-gray-50">
<div className="max-w-md w-full bg-white rounded-lg shadow-lg p-6 sm:p-8">
<div className="flex items-center justify-center w-12 h-12 sm:w-16 sm:h-16 bg-red-100 rounded-full mx-auto mb-4">
<AlertTriangle className="w-6 h-6 sm:w-8 sm:h-8 text-red-600" />
</div>
<h2 className="text-xl sm:text-2xl font-bold text-gray-900 text-center mb-2">
{isNotFound ? 'List Not Found' : 'Error Loading List'}
</h2>
<p className="text-sm sm:text-base text-gray-600 text-center mb-6">
{isNotFound
? 'The list you are looking for does not exist or has been deleted.'
: error.message || 'We encountered an error loading this list. Please try again.'}
</p>
<div className="flex flex-col sm:flex-row gap-3">
{!isNotFound && (
<button
onClick={reset}
className="flex-1 px-4 py-3 bg-blue-500 hover:bg-blue-600 active:bg-blue-700 text-white rounded-lg transition-colors font-medium text-sm sm:text-base tap-target"
>
Try Again
</button>
)}
<Link
href="/"
className="flex-1 px-4 py-3 bg-gray-200 hover:bg-gray-300 active:bg-gray-400 text-gray-700 rounded-lg transition-colors font-medium text-sm sm:text-base tap-target text-center"
>
Back to Lists
</Link>
</div>
</div>
</div>
)
}

View File

@@ -0,0 +1,55 @@
'use client'
import { useEffect } from 'react'
import { AlertTriangle } from 'lucide-react'
import { useRouter } from 'next/navigation'
export default function ItemError({
error,
reset,
}: {
error: Error & { digest?: string }
reset: () => void
}) {
const router = useRouter()
useEffect(() => {
console.error('Item page error:', error)
}, [error])
const isNotFound = error.message.includes('not found')
return (
<div className="min-h-screen flex items-center justify-center p-4 bg-gray-50">
<div className="max-w-md w-full bg-white rounded-lg shadow-lg p-6 sm:p-8">
<div className="flex items-center justify-center w-12 h-12 sm:w-16 sm:h-16 bg-red-100 rounded-full mx-auto mb-4">
<AlertTriangle className="w-6 h-6 sm:w-8 sm:h-8 text-red-600" />
</div>
<h2 className="text-xl sm:text-2xl font-bold text-gray-900 text-center mb-2">
{isNotFound ? 'Item Not Found' : 'Error Loading Item'}
</h2>
<p className="text-sm sm:text-base text-gray-600 text-center mb-6">
{isNotFound
? 'The item you are looking for does not exist or has been deleted.'
: error.message || 'We encountered an error loading this item. Please try again.'}
</p>
<div className="flex flex-col sm:flex-row gap-3">
{!isNotFound && (
<button
onClick={reset}
className="flex-1 px-4 py-3 bg-blue-500 hover:bg-blue-600 active:bg-blue-700 text-white rounded-lg transition-colors font-medium text-sm sm:text-base tap-target"
>
Try Again
</button>
)}
<button
onClick={() => router.back()}
className="flex-1 px-4 py-3 bg-gray-200 hover:bg-gray-300 active:bg-gray-400 text-gray-700 rounded-lg transition-colors font-medium text-sm sm:text-base tap-target"
>
Go Back
</button>
</div>
</div>
</div>
)
}