mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-11 16:05:44 +01:00
Add Error Boundary for Pages
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
//@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;
|
||||
@@ -7,6 +7,7 @@ import Subtitle from "./Subtitle";
|
||||
import injectSheet from "react-jss";
|
||||
import classNames from "classnames";
|
||||
import PageActions from "./PageActions";
|
||||
import PageErrorBoundary from "../errorboundary/PageErrorBoundary";
|
||||
|
||||
type Props = {
|
||||
title?: string,
|
||||
@@ -31,6 +32,7 @@ class Page extends React.Component<Props> {
|
||||
render() {
|
||||
const { error } = this.props;
|
||||
return (
|
||||
<PageErrorBoundary>
|
||||
<section className="section">
|
||||
<div className="container">
|
||||
{this.renderPageHeader()}
|
||||
@@ -38,6 +40,7 @@ class Page extends React.Component<Props> {
|
||||
{this.renderContent()}
|
||||
</div>
|
||||
</section>
|
||||
</PageErrorBoundary>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user