diff --git a/web/error-boundary.css b/web/error-boundary.css new file mode 100644 index 000000000..95ed099c9 --- /dev/null +++ b/web/error-boundary.css @@ -0,0 +1,9 @@ +.container { + height: 100vh; + display: flex; + flex-direction: column; + justify-content: center; + align-items: center; + color: var(--fg); + font-size: var(--m-font-16); +} diff --git a/web/error-boundary.react.js b/web/error-boundary.react.js new file mode 100644 index 000000000..517686db7 --- /dev/null +++ b/web/error-boundary.react.js @@ -0,0 +1,42 @@ +// @flow + +import * as React from 'react'; + +import type { ErrorInfo, ErrorData } from 'lib/types/report-types'; + +import css from './error-boundary.css'; + +type Props = { + +children: React.Node, +}; + +type State = { + +errorData: $ReadOnlyArray, + +showError: boolean, +}; + +class ErrorBoundary extends React.PureComponent { + state: State = { + errorData: [], + showError: false, + }; + + componentDidCatch(error: Error, info: ErrorInfo) { + this.setState(prevState => ({ + errorData: [...prevState.errorData, { error, info }], + })); + } + + render(): React.Node { + if (this.state.errorData.length > 0) { + return ( +
+

Something has gone wrong, please reload the page

+
+ ); + } + return this.props.children; + } +} + +export default ErrorBoundary; diff --git a/web/hot.js b/web/hot.js index 39d81e0f3..0252d697c 100644 --- a/web/hot.js +++ b/web/hot.js @@ -1,22 +1,25 @@ // @flow import * as React from 'react'; import { hot } from 'react-hot-loader/root'; import { Router, Route } from 'react-router'; import App from './app.react'; +import ErrorBoundary from './error-boundary.react'; import history from './router-history'; import Socket from './socket.react'; const RootComponent = () => ( - - - - + + + + + + ); const HotReloadingRootComponent: React.ComponentType<{}> = hot(RootComponent); export default HotReloadingRootComponent;