import { Component, type ErrorInfo, type ReactNode } from "react"; import { Alert, AlertDescription, AlertTitle } from "./ui/alert"; import { Button } from "./ui/button"; interface Props { children: ReactNode; } interface State { hasError: boolean; error?: Error; } export class ErrorBoundary extends Component { 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); } render() { if (this.state.hasError) { return (
Something went wrong The application encountered an error. Please try refreshing the page or contact support if the problem persists.
{process.env.NODE_ENV === "development" && this.state.error && (
{this.state.error.stack}
)}
); } return this.props.children; } }