2020-12-18 18:18:01 +00:00
|
|
|
import * as React from "react";
|
|
|
|
|
2020-12-18 18:25:27 +00:00
|
|
|
const cssStyle = require("./ErrorBoundary.scss");
|
|
|
|
|
2020-12-18 18:18:01 +00:00
|
|
|
interface ErrorBoundaryState {
|
|
|
|
errorOccurred: boolean
|
|
|
|
}
|
|
|
|
|
2021-03-12 16:46:27 +00:00
|
|
|
export class ErrorBoundary extends React.PureComponent<{}, ErrorBoundaryState> {
|
2020-12-18 18:25:27 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = { errorOccurred: false };
|
|
|
|
}
|
2020-12-18 18:18:01 +00:00
|
|
|
render() {
|
|
|
|
if(this.state.errorOccurred) {
|
2020-12-18 18:25:27 +00:00
|
|
|
return (
|
|
|
|
<div className={cssStyle.container}>
|
|
|
|
<div className={cssStyle.text}>A rendering error has occurred</div>
|
|
|
|
</div>
|
|
|
|
);
|
2021-01-05 13:46:09 +00:00
|
|
|
} else if(typeof this.props.children !== "undefined") {
|
2020-12-18 18:18:01 +00:00
|
|
|
return this.props.children;
|
2021-01-05 13:46:09 +00:00
|
|
|
} else {
|
|
|
|
return null;
|
2020-12-18 18:18:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidCatch(error: Error, errorInfo: React.ErrorInfo) {
|
2020-12-18 18:25:27 +00:00
|
|
|
/* TODO: Some kind of logging? */
|
2021-01-22 12:34:43 +00:00
|
|
|
console.error(error);
|
2020-12-18 18:18:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static getDerivedStateFromError() : Partial<ErrorBoundaryState> {
|
|
|
|
return { errorOccurred: true };
|
|
|
|
}
|
|
|
|
}
|