From 71ab2699d0e201b63f4728b2ed9e6c53dc5aff75 Mon Sep 17 00:00:00 2001 From: Justin Date: Fri, 4 Oct 2024 16:22:38 -0700 Subject: [PATCH] error bounds on content --- web/components/layout/auth/authLayout.tsx | 3 +- web/components/ui/error-boundary.tsx | 72 +++++++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 web/components/ui/error-boundary.tsx diff --git a/web/components/layout/auth/authLayout.tsx b/web/components/layout/auth/authLayout.tsx index ad48ffb495..a4824fa692 100644 --- a/web/components/layout/auth/authLayout.tsx +++ b/web/components/layout/auth/authLayout.tsx @@ -11,6 +11,7 @@ import AcceptTermsModal from "./AcceptTermsModal"; import DemoModal from "./DemoModal"; import MainContent from "./MainContent"; import Sidebar from "./Sidebar"; +import { ErrorBoundary } from "@/components/ui/error-boundary"; interface AuthLayoutProps { children: React.ReactNode; @@ -51,7 +52,7 @@ const AuthLayout = (props: AuthLayoutProps) => {
- {children} + {children}
diff --git a/web/components/ui/error-boundary.tsx b/web/components/ui/error-boundary.tsx new file mode 100644 index 0000000000..46771d0984 --- /dev/null +++ b/web/components/ui/error-boundary.tsx @@ -0,0 +1,72 @@ +import React, { Component, ErrorInfo, ReactNode } from "react"; +import { XCircleIcon } from "@heroicons/react/24/solid"; + +interface Props { + children?: ReactNode; +} + +interface State { + hasError: boolean; + error: Error | null; + errorInfo: ErrorInfo | null; +} + +export class ErrorBoundary extends Component { + public state: State = { + hasError: false, + error: null, + errorInfo: null, + }; + + public static getDerivedStateFromError(error: Error): State { + return { hasError: true, error, errorInfo: null }; + } + + public componentDidCatch(error: Error, errorInfo: ErrorInfo) { + this.setState({ errorInfo }); + console.error("Uncaught error:", error, errorInfo); + } + + public render() { + if (this.state.hasError) { + return ( +
+
+
+
+ +
+

+ Oops! Something went wrong. +

+

+ We apologize for the inconvenience. The error has been logged + and we'll look into it. +

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

+ Error details: +

+
+                    {this.state.error.toString()}
+                  
+
+ )} +
+ +
+
+
+
+ ); + } + + return this.props.children; + } +}