-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathBaseErrorBoundary.js
61 lines (48 loc) · 1.85 KB
/
BaseErrorBoundary.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import React from 'react';
import PropTypes from 'prop-types';
import BootSplash from '../../libs/BootSplash';
import GenericErrorPage from '../../pages/ErrorPage/GenericErrorPage';
const propTypes = {
/* A message posted to `logError` (along with error data) when this component intercepts an error */
errorMessage: PropTypes.string.isRequired,
/* A function to perform the actual logging since different platforms support different tools */
logError: PropTypes.func,
/* Actual content wrapped by this error boundary */
children: PropTypes.node.isRequired,
};
const defaultProps = {
logError: () => {},
};
/**
* This component captures an error in the child component tree and logs it to the server
* It can be used to wrap the entire app as well as to wrap specific parts for more granularity
* @see {@link https://reactjs.org/docs/error-boundaries.html#where-to-place-error-boundaries}
*/
class BaseErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = {hasError: false};
this.clearError = this.clearError.bind(this);
}
static getDerivedStateFromError() {
// Update state so the next render will show the fallback UI.
return {hasError: true};
}
componentDidCatch(error, errorInfo) {
this.props.logError(this.props.errorMessage, error, JSON.stringify(errorInfo));
// We hide the splash screen since the error might happened during app init
BootSplash.hide();
}
clearError() {
this.setState({hasError: false});
}
render() {
if (this.state.hasError) {
return <GenericErrorPage onRefresh={this.clearError} />;
}
return this.props.children;
}
}
BaseErrorBoundary.propTypes = propTypes;
BaseErrorBoundary.defaultProps = defaultProps;
export default BaseErrorBoundary;