diff --git a/package-lock.json b/package-lock.json index 940ff3c..762f59f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -52,6 +52,7 @@ "react": "^18.2.0", "react-dom": "^18.2.0", "react-dropzone": "^14.2.3", + "react-error-boundary": "^4.1.2", "react-i18next": "^16.5.3", "react-markdown": "^10.1.0", "react-router-dom": "^6.8.1", @@ -9834,6 +9835,18 @@ "react": ">= 16.8 || 18.0.0" } }, + "node_modules/react-error-boundary": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/react-error-boundary/-/react-error-boundary-4.1.2.tgz", + "integrity": "sha512-GQDxZ5Jd+Aq/qUxbCm1UtzmL/s++V7zKgE8yMktJiCQXCCFZnMZh9ng+6/Ne6PjNSXH0L9CjeOEREfRnq6Duag==", + "license": "MIT", + "dependencies": { + "@babel/runtime": "^7.12.5" + }, + "peerDependencies": { + "react": ">=16.13.1" + } + }, "node_modules/react-i18next": { "version": "16.5.3", "resolved": "https://registry.npmmirror.com/react-i18next/-/react-i18next-16.5.3.tgz", diff --git a/package.json b/package.json index 3c5e0c4..3331568 100644 --- a/package.json +++ b/package.json @@ -86,6 +86,7 @@ "react": "^18.2.0", "react-dom": "^18.2.0", "react-dropzone": "^14.2.3", + "react-error-boundary": "^4.1.2", "react-i18next": "^16.5.3", "react-markdown": "^10.1.0", "react-router-dom": "^6.8.1", diff --git a/src/components/ErrorBoundary.jsx b/src/components/ErrorBoundary.jsx index 5ebe7ca..b20c028 100644 --- a/src/components/ErrorBoundary.jsx +++ b/src/components/ErrorBoundary.jsx @@ -1,73 +1,77 @@ -import React from 'react'; +import React, { useCallback, useState } from 'react'; +import { ErrorBoundary as ReactErrorBoundary } from 'react-error-boundary'; -class ErrorBoundary extends React.Component { - constructor(props) { - super(props); - this.state = { hasError: false, error: null, errorInfo: null }; - } - - static getDerivedStateFromError(error) { - // Update state so the next render will show the fallback UI - return { hasError: true }; - } - - componentDidCatch(error, errorInfo) { - // Log the error details - console.error('ErrorBoundary caught an error:', error, errorInfo); - - // You can also log the error to an error reporting service here - this.setState({ - error: error, - errorInfo: errorInfo - }); - } - - render() { - if (this.state.hasError) { - // Fallback UI - return ( -
-
-
-
- - - -
-

- Something went wrong -

-
-
-

An error occurred while loading the chat interface.

- {this.props.showDetails && this.state.error && ( -
- Error Details -
-                    {this.state.error.toString()}
-                    {this.state.errorInfo && this.state.errorInfo.componentStack}
-                  
-
- )} -
-
- -
+function ErrorFallback({ error, resetErrorBoundary, showDetails, componentStack }) { + return ( +
+
+
+
+ + +
+

+ Something went wrong +

- ); - } - - return this.props.children; - } +
+

An error occurred while loading the chat interface.

+ {showDetails && error && ( +
+ Error Details +
+                {error.toString()}
+                {componentStack}
+              
+
+ )} +
+
+ +
+
+
+ ); } -export default ErrorBoundary; \ No newline at end of file +function ErrorBoundary({ children, showDetails = false, onRetry = undefined, resetKeys = undefined }) { + const [componentStack, setComponentStack] = useState(null); + + const handleError = useCallback((error, errorInfo) => { + console.error('ErrorBoundary caught an error:', error, errorInfo); + setComponentStack(errorInfo?.componentStack || null); + }, []); + + const handleReset = useCallback(() => { + setComponentStack(null); + onRetry?.(); + }, [onRetry]); + + const renderFallback = useCallback(({ error, resetErrorBoundary }) => ( + + ), [showDetails, componentStack]); + + return ( + + {children} + + ); +} + +export default ErrorBoundary;