From 92c15bd310cce031447b8a4347f9da0954a098e0 Mon Sep 17 00:00:00 2001 From: Yunus A M Date: Wed, 1 Nov 2023 01:32:40 +0530 Subject: [PATCH] feat: create base level error boundry component --- .../components/ErrorBoundry/ErrorBoundry.tsx | 44 ++++++ .../MetricsApplication/Tabs/Overview.tsx | 143 +++++++++--------- .../container/ServiceApplication/index.tsx | 9 +- frontend/src/index.tsx | 30 ++-- 4 files changed, 140 insertions(+), 86 deletions(-) create mode 100644 frontend/src/components/ErrorBoundry/ErrorBoundry.tsx diff --git a/frontend/src/components/ErrorBoundry/ErrorBoundry.tsx b/frontend/src/components/ErrorBoundry/ErrorBoundry.tsx new file mode 100644 index 00000000000..318e1c39df7 --- /dev/null +++ b/frontend/src/components/ErrorBoundry/ErrorBoundry.tsx @@ -0,0 +1,44 @@ +/* eslint-disable @typescript-eslint/ban-types */ +import { Component, ErrorInfo, ReactNode } from 'react'; + +type ErrorBoundaryProps = { + children: ReactNode; +}; + +type ErrorBoundaryState = { + hasError: boolean; +}; + +class ErrorBoundary extends Component { + constructor(props: ErrorBoundaryProps) { + super(props); + this.state = { hasError: false }; + } + + componentDidCatch(error: Error, errorInfo: ErrorInfo): void { + // Handle the error and update the state + this.setState({ hasError: true }); + // You can also log the error to a service like Sentry or report it to the server. + console.error(error, errorInfo); + } + + render(): JSX.Element { + const { hasError } = this.state; + const { children } = this.props; + + if (hasError) { + // Render a fallback UI when an error occurs + return ( +
+

Something went wrong

+

We apologize for the inconvenience.

+
+ ); + } + + // Render the children if no error occurred + return <> {children} ; + } +} + +export default ErrorBoundary; diff --git a/frontend/src/container/MetricsApplication/Tabs/Overview.tsx b/frontend/src/container/MetricsApplication/Tabs/Overview.tsx index d67053e1e0b..3ab9a600135 100644 --- a/frontend/src/container/MetricsApplication/Tabs/Overview.tsx +++ b/frontend/src/container/MetricsApplication/Tabs/Overview.tsx @@ -2,6 +2,7 @@ import getTopLevelOperations, { ServiceDataProps, } from 'api/metrics/getTopLevelOperations'; import { ActiveElement, Chart, ChartData, ChartEvent } from 'chart.js'; +import ErrorBoundary from 'components/ErrorBoundry/ErrorBoundry'; import { FeatureKeys } from 'constants/features'; import { QueryParams } from 'constants/query'; import { PANEL_TYPES } from 'constants/queryBuilder'; @@ -190,51 +191,25 @@ function Application(): JSX.Element { }; return ( - <> - - - - + + <> + + + + - - - - - - - - + - - - - - - - + + + + + + + + + + + + + + - - - {isSpanMetricEnabled ? : } - - - - + + + {isSpanMetricEnabled ? : } + + + + + ); } diff --git a/frontend/src/container/ServiceApplication/index.tsx b/frontend/src/container/ServiceApplication/index.tsx index 5cfce03ee2b..c5b8bfc1468 100644 --- a/frontend/src/container/ServiceApplication/index.tsx +++ b/frontend/src/container/ServiceApplication/index.tsx @@ -1,3 +1,4 @@ +import ErrorBoundary from 'components/ErrorBoundry/ErrorBoundry'; import { FeatureKeys } from 'constants/features'; import useFeatureFlag from 'hooks/useFeatureFlag'; @@ -10,9 +11,11 @@ function Services(): JSX.Element { ?.active; return ( - - {isSpanMetricEnabled ? : } - + + + {isSpanMetricEnabled ? : } + + ); } diff --git a/frontend/src/index.tsx b/frontend/src/index.tsx index 712fb4cc582..bdad4411261 100644 --- a/frontend/src/index.tsx +++ b/frontend/src/index.tsx @@ -1,6 +1,7 @@ import './ReactI18'; import AppRoutes from 'AppRoutes'; +import ErrorBoundary from 'components/ErrorBoundry/ErrorBoundry'; import GlobalStyles from 'globalStyles'; import { ThemeProvider } from 'hooks/useDarkMode'; import { createRoot } from 'react-dom/client'; @@ -24,18 +25,21 @@ if (container) { const root = createRoot(container); root.render( - - - - - - - - {process.env.NODE_ENV === 'development' && ( - - )} - - - , + + + + + + + + + {process.env.NODE_ENV === 'development' && ( + + )} + + + + , + , ); }