|
|
|
'use client'; |
|
|
|
import React, { ReactNode, ErrorInfo, Component } from 'react'; |
|
|
|
interface ErrorBoundaryProps { |
|
children: ReactNode; |
|
fallback?: ReactNode; |
|
} |
|
|
|
interface ErrorBoundaryState { |
|
hasError: boolean; |
|
} |
|
|
|
class ErrorBoundary extends Component<ErrorBoundaryProps, ErrorBoundaryState> { |
|
constructor(props: ErrorBoundaryProps) { |
|
super(props); |
|
this.state = { hasError: false }; |
|
} |
|
|
|
static getDerivedStateFromError(_: Error): ErrorBoundaryState { |
|
|
|
return { hasError: true }; |
|
} |
|
|
|
componentDidCatch(error: Error, errorInfo: ErrorInfo): void { |
|
|
|
console.error("Error caught by ErrorBoundary:", error, errorInfo); |
|
} |
|
|
|
render(): ReactNode { |
|
if (this.state.hasError) { |
|
|
|
return this.props.fallback || <div>Something went wrong.</div>; |
|
} |
|
|
|
return this.props.children; |
|
} |
|
} |
|
|
|
export default ErrorBoundary; |