From 13e7ae24bd0c5f4fb4fa52847d8f0b16396f0b1d Mon Sep 17 00:00:00 2001 From: Mohamed Karray Date: Wed, 13 Mar 2019 21:41:08 +0100 Subject: [PATCH] Add Error Boundary for - the Page component - the Main component --- .../ui-components/src/ErrorBoundary.js | 28 +++ .../src/errorboundary/PageErrorBoundary.js | 32 --- .../packages/ui-components/src/index.js | 1 + .../packages/ui-components/src/layout/Page.js | 6 +- scm-ui/src/containers/Main.js | 182 +++++++++--------- 5 files changed, 124 insertions(+), 125 deletions(-) create mode 100644 scm-ui-components/packages/ui-components/src/ErrorBoundary.js delete mode 100644 scm-ui-components/packages/ui-components/src/errorboundary/PageErrorBoundary.js diff --git a/scm-ui-components/packages/ui-components/src/ErrorBoundary.js b/scm-ui-components/packages/ui-components/src/ErrorBoundary.js new file mode 100644 index 0000000000..f955f03c49 --- /dev/null +++ b/scm-ui-components/packages/ui-components/src/ErrorBoundary.js @@ -0,0 +1,28 @@ +// @flow +import React from "react"; +import ErrorNotification from "./ErrorNotification"; + +class ErrorBoundary extends React.Component { + constructor(props) { + super(props); + this.state = { error: null, errorInfo: null }; + } + + componentDidCatch(error, errorInfo) { + // Catch errors in any components below and re-render with error message + this.setState({ + error: error, + errorInfo: errorInfo + }); + } + + render() { + if (this.state.errorInfo) { + return ( + + ); + } + return this.props.children; + } +} +export default ErrorBoundary; diff --git a/scm-ui-components/packages/ui-components/src/errorboundary/PageErrorBoundary.js b/scm-ui-components/packages/ui-components/src/errorboundary/PageErrorBoundary.js deleted file mode 100644 index 2be112455e..0000000000 --- a/scm-ui-components/packages/ui-components/src/errorboundary/PageErrorBoundary.js +++ /dev/null @@ -1,32 +0,0 @@ -//@flow - -import * as React from "react"; - - -class PageErrorBoundary extends React.Component { - constructor(props) { - super(props); - this.state = { hasError: false }; - } - - /** - * This lifecycle is invoked after an error has been thrown by a descendant component. - * It receives the error that was thrown as a parameter and should return a value to update state. - * @param error - * @returns {{hasError: boolean}} - */ - static getDerivedStateFromError(error) { - // Update state so the next render will show the fallback UI. - return { hasError: true }; - } - - render() { - if (this.state.hasError) { - return

Something went wrong.

; - } - - return this.props.children; - } -} - -export default PageErrorBoundary; diff --git a/scm-ui-components/packages/ui-components/src/index.js b/scm-ui-components/packages/ui-components/src/index.js index 39f3769441..61a40eddda 100644 --- a/scm-ui-components/packages/ui-components/src/index.js +++ b/scm-ui-components/packages/ui-components/src/index.js @@ -25,6 +25,7 @@ export { default as Tooltip } from "./Tooltip"; export { getPageFromMatch } from "./urls"; export { default as Autocomplete} from "./Autocomplete"; export { default as BranchSelector } from "./BranchSelector"; +export { default as ErrorBoundary } from "./ErrorBoundary"; export { apiClient } from "./apiclient.js"; export * from "./errors"; diff --git a/scm-ui-components/packages/ui-components/src/layout/Page.js b/scm-ui-components/packages/ui-components/src/layout/Page.js index 37913a009c..2cab7a1b0a 100644 --- a/scm-ui-components/packages/ui-components/src/layout/Page.js +++ b/scm-ui-components/packages/ui-components/src/layout/Page.js @@ -7,7 +7,7 @@ import Subtitle from "./Subtitle"; import injectSheet from "react-jss"; import classNames from "classnames"; import PageActions from "./PageActions"; -import PageErrorBoundary from "../errorboundary/PageErrorBoundary"; +import ErrorBoundary from "../ErrorBoundary"; type Props = { title?: string, @@ -32,7 +32,7 @@ class Page extends React.Component { render() { const { error } = this.props; return ( - +
{this.renderPageHeader()} @@ -40,7 +40,7 @@ class Page extends React.Component { {this.renderContent()}
-
+ ); } diff --git a/scm-ui/src/containers/Main.js b/scm-ui/src/containers/Main.js index 6ce4e982fa..9036f769f2 100644 --- a/scm-ui/src/containers/Main.js +++ b/scm-ui/src/containers/Main.js @@ -9,7 +9,7 @@ import Users from "../users/containers/Users"; import Login from "../containers/Login"; import Logout from "../containers/Logout"; -import { ProtectedRoute } from "@scm-manager/ui-components"; +import {ErrorBoundary, ProtectedRoute} from "@scm-manager/ui-components"; import {binder, ExtensionPoint } from "@scm-manager/ui-extensions"; import AddUser from "../users/containers/AddUser"; @@ -38,97 +38,99 @@ class Main extends React.Component { url = redirectUrlFactory(this.props); } return ( -
- - - - - - - - - - - - + +
+ + + + + + + + + + + + - - - - - - + + + + + + - - -
+ +
+
+ ); } }