Add error boundary

This commit is contained in:
MartinBraquet
2026-03-01 02:56:48 +01:00
parent 8ba8604d83
commit b5b2bafc78

View File

@@ -0,0 +1,69 @@
import {Component, ErrorInfo, ReactNode} from 'react'
import {Button} from 'web/components/buttons/button'
interface Props {
children?: ReactNode
fallback?: ReactNode
}
interface State {
hasError: boolean
error?: Error
}
export class ErrorBoundary extends Component<Props, State> {
constructor(props: Props) {
super(props)
this.state = {hasError: false}
}
static getDerivedStateFromError(error: Error): State {
return {hasError: true, error}
}
componentDidCatch(error: Error, errorInfo: ErrorInfo) {
console.error('ErrorBoundary caught an error:', error, errorInfo)
}
handleReset = () => {
this.setState({hasError: false, error: undefined})
window.location.reload()
}
render() {
if (this.state.hasError) {
if (this.props.fallback) {
return this.props.fallback
}
return (
<div className="flex min-h-screen flex-col items-center justify-center bg-gray-50 p-4">
<div className="max-w-md text-center">
<h1 className="mb-4 text-2xl font-bold text-gray-900">Something went wrong</h1>
<p className="mb-6 text-gray-600">
We apologize for the inconvenience. An unexpected error occurred.
</p>
{this.state.error && (
<details className="mb-6 text-left">
<summary className="cursor-pointer text-sm text-gray-500 hover:text-gray-700">
Error details
</summary>
<pre className="mt-2 overflow-auto rounded bg-gray-100 p-2 text-xs">
{this.state.error.message}
</pre>
</details>
)}
<div className="flex gap-3 justify-center">
<Button onClick={this.handleReset}>Reload page</Button>
<Button color="gray" onClick={() => window.location.replace('/')}>
Go home
</Button>
</div>
</div>
</div>
)
}
return this.props.children
}
}