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:
81
components/ErrorBoundary.tsx
Normal file
81
components/ErrorBoundary.tsx
Normal file
@@ -0,0 +1,81 @@
|
||||
'use client'
|
||||
|
||||
import React, { Component, ReactNode } from 'react'
|
||||
import { AlertTriangle } from 'lucide-react'
|
||||
|
||||
interface Props {
|
||||
children: ReactNode
|
||||
fallback?: ReactNode
|
||||
}
|
||||
|
||||
interface State {
|
||||
hasError: boolean
|
||||
error: Error | null
|
||||
}
|
||||
|
||||
export class ErrorBoundary extends Component<Props, State> {
|
||||
constructor(props: Props) {
|
||||
super(props)
|
||||
this.state = { hasError: false, error: null }
|
||||
}
|
||||
|
||||
static getDerivedStateFromError(error: Error): State {
|
||||
return { hasError: true, error }
|
||||
}
|
||||
|
||||
componentDidCatch(error: Error, errorInfo: React.ErrorInfo) {
|
||||
console.error('Error caught by boundary:', error, errorInfo)
|
||||
}
|
||||
|
||||
render() {
|
||||
if (this.state.hasError) {
|
||||
if (this.props.fallback) {
|
||||
return this.props.fallback
|
||||
}
|
||||
|
||||
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">
|
||||
We encountered an unexpected error. Please try refreshing the page.
|
||||
</p>
|
||||
{this.state.error && (
|
||||
<details className="mb-6 p-3 sm:p-4 bg-gray-50 rounded-lg text-xs sm:text-sm">
|
||||
<summary className="cursor-pointer font-medium text-gray-700 mb-2">
|
||||
Error details
|
||||
</summary>
|
||||
<pre className="text-red-600 overflow-auto whitespace-pre-wrap break-words">
|
||||
{this.state.error.message}
|
||||
</pre>
|
||||
</details>
|
||||
)}
|
||||
<div className="flex flex-col sm:flex-row gap-3">
|
||||
<button
|
||||
onClick={() => window.location.reload()}
|
||||
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"
|
||||
>
|
||||
Refresh Page
|
||||
</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>
|
||||
)
|
||||
}
|
||||
|
||||
return this.props.children
|
||||
}
|
||||
}
|
||||
|
||||
export default ErrorBoundary
|
||||
Reference in New Issue
Block a user