diff --git a/client/src/components/ErrorBoundary.jsx b/client/src/components/ErrorBoundary.jsx new file mode 100644 index 00000000..9ae96ce9 --- /dev/null +++ b/client/src/components/ErrorBoundary.jsx @@ -0,0 +1,61 @@ +import React from 'react'; + +class ErrorBoundary extends React.Component { + constructor(props) { + super(props); + this.state = { hasError: false, error: null }; + } + + static getDerivedStateFromError(error) { + // Update state so the next render will show the fallback UI. + return { hasError: true, error }; + } + + componentDidCatch(error, errorInfo) { + // You can also log the error to an error reporting service + console.error("ErrorBoundary caught an error:", error, errorInfo); + } + + handleReload = () => { + window.location.reload(); + }; + + render() { + if (this.state.hasError) { + return ( +
+
+ + + +

Something went wrong

+

+ {this.state.error?.message || "An unexpected application error occurred."} +

+ +
+
+ ); + } + + return this.props.children; + } +} + +export default ErrorBoundary; diff --git a/client/src/main.jsx b/client/src/main.jsx index bcb0409b..ff2bca71 100644 --- a/client/src/main.jsx +++ b/client/src/main.jsx @@ -3,12 +3,15 @@ import React from "react"; import ReactDOM from "react-dom/client"; import App from "./App.jsx"; import { ThemeProvider } from "./components/ThemeContext.jsx"; +import ErrorBoundary from "./components/ErrorBoundary.jsx"; import "./styles.css"; ReactDOM.createRoot(document.getElementById("root")).render( - - - + + + + + ); \ No newline at end of file