59 lines
1.4 KiB
TypeScript
59 lines
1.4 KiB
TypeScript
"use client"
|
|
|
|
import { Component, ReactNode } from "react"
|
|
import { Button } from "@/components/ui/button"
|
|
|
|
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("ErrorBoundary caught an error:", error, errorInfo)
|
|
}
|
|
|
|
render() {
|
|
if (this.state.hasError) {
|
|
if (this.props.fallback) {
|
|
return this.props.fallback
|
|
}
|
|
|
|
return (
|
|
<div className="container mx-auto px-4 py-16">
|
|
<div className="text-center">
|
|
<h1 className="text-2xl font-bold mb-4">Something went wrong</h1>
|
|
<p className="text-muted-foreground mb-4">
|
|
{this.state.error?.message || "An unexpected error occurred"}
|
|
</p>
|
|
<Button
|
|
onClick={() => {
|
|
this.setState({ hasError: false, error: null })
|
|
window.location.reload()
|
|
}}
|
|
variant="brand"
|
|
>
|
|
Reload Page
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
return this.props.children
|
|
}
|
|
}
|