From 6b84500cd98b2a3b3de30df9f5fd9661019cc859 Mon Sep 17 00:00:00 2001 From: KGFCH2 Date: Sun, 31 May 2026 21:15:24 +0530 Subject: [PATCH] feat: add React ErrorBoundary to prevent application crashes --- .../src/components/shared/ErrorBoundary.jsx | 71 +++++++++++++++++++ frontend/src/main.jsx | 5 +- 2 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 frontend/src/components/shared/ErrorBoundary.jsx diff --git a/frontend/src/components/shared/ErrorBoundary.jsx b/frontend/src/components/shared/ErrorBoundary.jsx new file mode 100644 index 0000000..9379805 --- /dev/null +++ b/frontend/src/components/shared/ErrorBoundary.jsx @@ -0,0 +1,71 @@ +import React from 'react'; + +class ErrorBoundary extends React.Component { + constructor(props) { + super(props); + this.state = { hasError: false, error: null, errorInfo: null }; + } + + static getDerivedStateFromError(error) { + return { hasError: true, error }; + } + + componentDidCatch(error, errorInfo) { + console.error("ErrorBoundary caught an error", error, errorInfo); + this.setState({ errorInfo }); + } + + handleReset = () => { + this.setState({ hasError: false, error: null, errorInfo: null }); + window.location.href = '/'; + }; + + render() { + if (this.state.hasError) { + return ( +
+
+
+ + + +
+ +

Something went wrong

+

+ CodeLens encountered an unexpected error. Don't worry, your progress has not been lost. +

+ + {this.state.error && ( +
+

{this.state.error.toString()}

+ {this.state.errorInfo && ( +
{this.state.errorInfo.componentStack}
+ )} +
+ )} + +
+ + +
+
+
+ ); + } + + return this.props.children; + } +} + +export default ErrorBoundary; diff --git a/frontend/src/main.jsx b/frontend/src/main.jsx index 5c50cc5..4042d7f 100644 --- a/frontend/src/main.jsx +++ b/frontend/src/main.jsx @@ -1,10 +1,13 @@ import { StrictMode } from 'react' import { createRoot } from 'react-dom/client' import App from './App' +import ErrorBoundary from './components/shared/ErrorBoundary' import './index.css' createRoot(document.getElementById('root')).render( - + + + , )