From 0c4ba7192260f92ecddd519c07c6af6833437f5b Mon Sep 17 00:00:00 2001 From: Anoop M D Date: Sat, 30 Sep 2023 01:32:05 +0530 Subject: [PATCH] feat(#229): added error boundary to capture error trace and allow users to continue using the app --- .../src/pages/ErrorBoundary/index.js | 44 +++++++++++++++++++ packages/bruno-app/src/pages/_app.js | 37 +++++++++------- 2 files changed, 64 insertions(+), 17 deletions(-) create mode 100644 packages/bruno-app/src/pages/ErrorBoundary/index.js diff --git a/packages/bruno-app/src/pages/ErrorBoundary/index.js b/packages/bruno-app/src/pages/ErrorBoundary/index.js new file mode 100644 index 000000000..3b45122ab --- /dev/null +++ b/packages/bruno-app/src/pages/ErrorBoundary/index.js @@ -0,0 +1,44 @@ +import React from 'react'; + +class ErrorBoundary extends React.Component { + constructor(props) { + super(props); + + this.state = { hasError: false }; + } + componentDidMount() { + // Add a global error event listener to capture client-side errors + window.onerror = (message, source, lineno, colno, error) => { + this.setState({ hasError: true, error }); + }; + } + componentDidCatch(error, errorInfo) { + console.log({ error, errorInfo }); + } + render() { + if (this.state.hasError) { + return ( +
+
+

Oops! Something went wrong

+

{this.state.error && this.state.error.toString()}

+ {this.state.error && this.state.error.stack && ( +
{this.state.error.stack}
+ )} + +
+
+ ); + } + return this.props.children; + } +} + +export default ErrorBoundary; diff --git a/packages/bruno-app/src/pages/_app.js b/packages/bruno-app/src/pages/_app.js index 382b95093..ab2692529 100644 --- a/packages/bruno-app/src/pages/_app.js +++ b/packages/bruno-app/src/pages/_app.js @@ -7,6 +7,7 @@ import { PreferencesProvider } from 'providers/Preferences'; import ReduxStore from 'providers/ReduxStore'; import ThemeProvider from 'providers/Theme/index'; +import ErrorBoundary from './ErrorBoundary'; import '../styles/app.scss'; import '../styles/globals.css'; @@ -41,23 +42,25 @@ function MyApp({ Component, pageProps }) { } return ( - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + ); }