diff --git a/src/components/ErrorBoundary.tsx b/src/components/ErrorBoundary.tsx new file mode 100644 index 0000000..5bbce6b --- /dev/null +++ b/src/components/ErrorBoundary.tsx @@ -0,0 +1,66 @@ +"use client"; + +import React, { Component, ErrorInfo, ReactNode } from "react"; +import { Button } from "@/components/ui/button"; + +interface Props { + children: ReactNode; +} + +interface State { + hasError: boolean; + error: Error | null; +} + +class ErrorBoundary extends Component { + constructor(props: Props) { + super(props); + this.state = { hasError: false, error: null }; + } + + static getDerivedStateFromError(error: Error): State { + return { hasError: true, error }; + } + + componentDidCatch(error: Error, errorInfo: ErrorInfo): void { + console.error("ErrorBoundary caught an error:", error, errorInfo); + } + + handleTryAgain = (): void => { + this.setState({ hasError: false, error: null }); + }; + + handleReload = (): void => { + window.location.reload(); + }; + + render(): ReactNode { + if (this.state.hasError) { + return ( +
+
+

+ Something went wrong +

+ {this.state.error && ( +

+ {this.state.error.message} +

+ )} +
+
+ + +
+
+ ); + } + + return this.props.children; + } +} + +export { ErrorBoundary }; +export default ErrorBoundary;