mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-08 14:35:45 +01:00
Add Error Boundary for
- the Page component - the Main component
This commit is contained in:
@@ -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 (
|
||||||
|
<ErrorNotification error={this.state.error} />
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return this.props.children;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export default ErrorBoundary;
|
||||||
@@ -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 <h1>Something went wrong.</h1>;
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.props.children;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export default PageErrorBoundary;
|
|
||||||
@@ -25,6 +25,7 @@ export { default as Tooltip } from "./Tooltip";
|
|||||||
export { getPageFromMatch } from "./urls";
|
export { getPageFromMatch } from "./urls";
|
||||||
export { default as Autocomplete} from "./Autocomplete";
|
export { default as Autocomplete} from "./Autocomplete";
|
||||||
export { default as BranchSelector } from "./BranchSelector";
|
export { default as BranchSelector } from "./BranchSelector";
|
||||||
|
export { default as ErrorBoundary } from "./ErrorBoundary";
|
||||||
|
|
||||||
export { apiClient } from "./apiclient.js";
|
export { apiClient } from "./apiclient.js";
|
||||||
export * from "./errors";
|
export * from "./errors";
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import Subtitle from "./Subtitle";
|
|||||||
import injectSheet from "react-jss";
|
import injectSheet from "react-jss";
|
||||||
import classNames from "classnames";
|
import classNames from "classnames";
|
||||||
import PageActions from "./PageActions";
|
import PageActions from "./PageActions";
|
||||||
import PageErrorBoundary from "../errorboundary/PageErrorBoundary";
|
import ErrorBoundary from "../ErrorBoundary";
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
title?: string,
|
title?: string,
|
||||||
@@ -32,7 +32,7 @@ class Page extends React.Component<Props> {
|
|||||||
render() {
|
render() {
|
||||||
const { error } = this.props;
|
const { error } = this.props;
|
||||||
return (
|
return (
|
||||||
<PageErrorBoundary>
|
<ErrorBoundary>
|
||||||
<section className="section">
|
<section className="section">
|
||||||
<div className="container">
|
<div className="container">
|
||||||
{this.renderPageHeader()}
|
{this.renderPageHeader()}
|
||||||
@@ -40,7 +40,7 @@ class Page extends React.Component<Props> {
|
|||||||
{this.renderContent()}
|
{this.renderContent()}
|
||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
</PageErrorBoundary>
|
</ErrorBoundary>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import Users from "../users/containers/Users";
|
|||||||
import Login from "../containers/Login";
|
import Login from "../containers/Login";
|
||||||
import Logout from "../containers/Logout";
|
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 {binder, ExtensionPoint } from "@scm-manager/ui-extensions";
|
||||||
|
|
||||||
import AddUser from "../users/containers/AddUser";
|
import AddUser from "../users/containers/AddUser";
|
||||||
@@ -38,6 +38,7 @@ class Main extends React.Component<Props> {
|
|||||||
url = redirectUrlFactory(this.props);
|
url = redirectUrlFactory(this.props);
|
||||||
}
|
}
|
||||||
return (
|
return (
|
||||||
|
<ErrorBoundary>
|
||||||
<div className="main">
|
<div className="main">
|
||||||
<Switch>
|
<Switch>
|
||||||
<Redirect exact from="/" to={url}/>
|
<Redirect exact from="/" to={url}/>
|
||||||
@@ -129,6 +130,7 @@ class Main extends React.Component<Props> {
|
|||||||
/>
|
/>
|
||||||
</Switch>
|
</Switch>
|
||||||
</div>
|
</div>
|
||||||
|
</ErrorBoundary>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user