From 0225e6fabbd58723cd2ffaedb329f46152b5888d Mon Sep 17 00:00:00 2001 From: Daniel Hougaard Date: Fri, 20 Sep 2024 01:20:54 +0400 Subject: [PATCH] feat: added error boundary --- .../src/layouts/AppLayout/ErrorBoundary.tsx | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 frontend/src/layouts/AppLayout/ErrorBoundary.tsx diff --git a/frontend/src/layouts/AppLayout/ErrorBoundary.tsx b/frontend/src/layouts/AppLayout/ErrorBoundary.tsx new file mode 100644 index 000000000..cce9885ab --- /dev/null +++ b/frontend/src/layouts/AppLayout/ErrorBoundary.tsx @@ -0,0 +1,86 @@ +import React, { ErrorInfo, ReactNode } from "react"; +import { useRouter } from "next/router"; +import { faBugs } from "@fortawesome/free-solid-svg-icons"; +import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; + +interface ErrorBoundaryProps { + children: ReactNode; +} + +interface ErrorBoundaryState { + hasError: boolean; + error: Error | null; +} + +const ErrorPage = ({ error }: { error: Error | null }) => { + const router = useRouter(); + const currentUrl = router?.asPath?.split("?")?.[0]; + + return ( +
+
+ +

+ Something unexpected went wrong. Please contact{" "} + + support@infisical.com + {" "} + if the issue persists. +

+ + {error && ( +
+
+

Error details:

+

+ Please provide this error message when contacting support, as it will help us + diagnose the issue at hand. +

+
+

+ + {currentUrl}, {error?.message} + +

+
+ )} +
+
+ ); +}; + +class ErrorBoundary extends React.Component { + constructor(props: ErrorBoundaryProps) { + super(props); + this.state = { hasError: false, error: null }; + } + + static getDerivedStateFromError(error: Error): ErrorBoundaryState { + return { hasError: true, error }; + } + + componentDidCatch(error: Error, errorInfo: ErrorInfo): void { + console.error("Error caught by ErrorBoundary:", error, errorInfo); + } + + render(): ReactNode { + const { hasError, error } = this.state; + const { children } = this.props; + + if (hasError) { + return ; + } + return children; + } +} + +const ErrorBoundaryWrapper: React.FC = ({ children }) => { + return {children}; +}; + +export default ErrorBoundaryWrapper;