diff options
Diffstat (limited to 'components/error-boundary.tsx')
| -rw-r--r-- | components/error-boundary.tsx | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/components/error-boundary.tsx b/components/error-boundary.tsx new file mode 100644 index 00000000..41334eb7 --- /dev/null +++ b/components/error-boundary.tsx @@ -0,0 +1,45 @@ +"use client"; + +import * as React from "react"; + +interface ErrorBoundaryProps { + children: React.ReactNode; + fallback: React.ComponentType<{ error: Error; reset: () => void }>; +} + +interface ErrorBoundaryState { + hasError: boolean; + error: Error | null; +} + +export class ErrorBoundary extends React.Component< + ErrorBoundaryProps, + ErrorBoundaryState +> { + constructor(props: ErrorBoundaryProps) { + super(props); + this.state = { hasError: false, error: null }; + } + + static getDerivedStateFromError(error: Error): ErrorBoundaryState { + return { hasError: true, error }; + } + + componentDidCatch(error: Error, errorInfo: React.ErrorInfo) { + console.error("Dashboard error boundary caught an error:", error, errorInfo); + } + + render() { + if (this.state.hasError && this.state.error) { + const Fallback = this.props.fallback; + return ( + <Fallback + error={this.state.error} + reset={() => this.setState({ hasError: false, error: null })} + /> + ); + } + + return this.props.children; + } +}
\ No newline at end of file |
