Skip to content

Free Motion Capture for Everyone πŸ’€βœ¨

TypeScript

add error boundary for router components

jonmatthisdd4d1a086c55
diff --git a/freemocap-ui/src/components/common/ErrorBoundary.tsx b/freemocap-ui/src/components/common/ErrorBoundary.tsx
new file mode 100644
index 00000000..0b82a502
--- /dev/null
+++ b/freemocap-ui/src/components/common/ErrorBoundary.tsx
@@ -0,0 +1,55 @@
+import React from "react";
+
+// Define the types for props and state
+interface ErrorBoundaryProps {
+ children: React.ReactNode;
+}
+
+interface ErrorBoundaryState {
+ hasError: boolean;
+ error: Error | null;
+ errorInfo: React.ErrorInfo | null;
+}
+
+class ErrorBoundary extends React.Component<ErrorBoundaryProps, ErrorBoundaryState> {
+ constructor(props: ErrorBoundaryProps) {
+ super(props);
+ this.state = { hasError: false, error: null, errorInfo: null };
+ }
+
+ static getDerivedStateFromError(error: Error): Partial<ErrorBoundaryState> {
+ // Update state so the next render shows the fallback UI.
+ return { hasError: true };
+ }
+
+ componentDidCatch(error: Error, errorInfo: React.ErrorInfo) {
+ // You can also log the error to an error reporting service
+ console.error('Error caught by ErrorBoundary', error, errorInfo);
+ // Store the error and errorInfo in state
+ this.setState({ error, errorInfo });
+ }
+
+ render() {
+ if (this.state.hasError) {
+ // Render a custom fallback UI with error details
+ return (
+ <div>
+ <h1>Something went wrong :(</h1>
+ <details style={{ whiteSpace: 'pre-wrap' }}>
+ {this.state.error && this.state.error.toString()}
+ <br />
+ {this.state.errorInfo && this.state.errorInfo.componentStack}
+ </details>
+ </div>
+ );
+ }
+
+ return this.props.children;
+ }
+}
+
+export default ErrorBoundary;
+// // Usage
+// <ErrorBoundary>
+// <ThreeJSCanvas3d />
+// </ErrorBoundary>
diff --git a/freemocap-ui/src/layout/content/BaseContent.tsx b/freemocap-ui/src/layout/content/BaseContent.tsx
index 80d276dc..62067a25 100644
--- a/freemocap-ui/src/layout/content/BaseContent.tsx
+++ b/freemocap-ui/src/layout/content/BaseContent.tsx
@@ -1,17 +1,21 @@
import React from 'react';
-import {Header} from "../../components/ui-components/Header";
import Box from "@mui/material/Box";
-import {Router} from "../routing/router";
+import ErrorBoundary from "@/components/common/ErrorBoundary";
+import {Router} from "@/layout/routing/router";
export const BaseContent = () => {
return (
<React.Fragment>
{/*<Header title="FreeeeMoCap " onDrawerToggle={() => {}}/>*/}
- <Box sx={{py: 6,
+ <Box sx={{
+ py: 6,
px: 4,
flex: 1,
- bgcolor: '#173f70', border: '1px solid charcoal'}}>
- <Router/>
+ bgcolor: '#113157', border: '1px solid charcoal'
+ }}>
+ <ErrorBoundary>
+ <Router/>
+ </ErrorBoundary>
</Box>
</React.Fragment>
)