forked from Gerome-Elassaad/CodingIT
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror-boundary.tsx
More file actions
140 lines (125 loc) · 4.06 KB
/
Copy patherror-boundary.tsx
File metadata and controls
140 lines (125 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
'use client'
import React from 'react'
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'
import { Button } from '@/components/ui/button'
import { AlertTriangle, RefreshCw } from 'lucide-react'
interface ErrorBoundaryState {
hasError: boolean
error?: Error
}
interface ErrorBoundaryProps {
children: React.ReactNode
fallback?: React.ReactNode
}
class ErrorBoundary extends React.Component<ErrorBoundaryProps, ErrorBoundaryState> {
constructor(props: ErrorBoundaryProps) {
super(props)
this.state = { hasError: false }
}
static getDerivedStateFromError(error: Error): ErrorBoundaryState {
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 (
<Card className="border-red-200">
<CardHeader>
<CardTitle className="flex items-center gap-2 text-red-600">
<AlertTriangle className="h-5 w-5" />
Something went wrong
</CardTitle>
<CardDescription>
An error occurred while loading this section. This might be due to missing configuration or network issues.
</CardDescription>
</CardHeader>
<CardContent>
<div className="space-y-4">
{process.env.NODE_ENV === 'development' && this.state.error && (
<details className="text-sm">
<summary className="cursor-pointer text-red-600 font-medium">
Error details (development only)
</summary>
<pre className="mt-2 p-2 bg-red-50 rounded text-xs overflow-auto">
{this.state.error.message}
{this.state.error.stack}
</pre>
</details>
)}
<div className="flex gap-2">
<Button
onClick={() => this.setState({ hasError: false })}
variant="outline"
size="sm"
>
<RefreshCw className="h-4 w-4 mr-2" />
Try again
</Button>
<Button
onClick={() => window.location.reload()}
size="sm"
>
Reload page
</Button>
</div>
</div>
</CardContent>
</Card>
)
}
return this.props.children
}
}
export default ErrorBoundary
// Hook version for functional components
export function useErrorHandler() {
return (error: Error, errorInfo?: any) => {
console.error('Error:', error, errorInfo)
// You could send this to an error reporting service
}
}
// Simple loading fallback component
export function LoadingFallback({ message = "Loading..." }: { message?: string }) {
return (
<div className="flex items-center justify-center py-8">
<div className="text-center">
<RefreshCw className="h-6 w-6 animate-spin mx-auto mb-2" />
<p className="text-sm text-muted-foreground">{message}</p>
</div>
</div>
)
}
// Settings section wrapper with error boundary
export function SettingsSection({ children, title, description }: {
children: React.ReactNode
title?: string
description?: string
}) {
return (
<ErrorBoundary
fallback={
<Card className="border-yellow-200">
<CardHeader>
<CardTitle className="flex items-center gap-2 text-yellow-600">
<AlertTriangle className="h-5 w-5" />
{title || 'Section'} Unavailable
</CardTitle>
<CardDescription>
{description || 'This section could not be loaded. Some features may require additional setup.'}
</CardDescription>
</CardHeader>
</Card>
}
>
{children}
</ErrorBoundary>
)
}