2018-08-31 20:53:45 +03:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
|
|
|
export default class ErrorBoundary extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
hasError: false,
|
|
|
|
error: null,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidCatch(error) {
|
|
|
|
this.setState({
|
|
|
|
hasError: true,
|
|
|
|
error,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { children, errorClasses, message } = this.props;
|
|
|
|
const { hasError, error } = this.state;
|
|
|
|
|
|
|
|
if (hasError) {
|
2018-11-16 11:28:34 +03:00
|
|
|
return (
|
|
|
|
<span className={errorClasses}>
|
|
|
|
{message} {error.toString()}
|
|
|
|
</span>
|
|
|
|
);
|
2018-08-31 20:53:45 +03:00
|
|
|
}
|
|
|
|
return children;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorBoundary.propTypes = {
|
2018-11-16 11:28:34 +03:00
|
|
|
children: PropTypes.oneOfType([PropTypes.object, PropTypes.bool]),
|
2018-08-31 20:53:45 +03:00
|
|
|
errorClasses: PropTypes.string,
|
|
|
|
message: PropTypes.string,
|
|
|
|
};
|
|
|
|
|
|
|
|
ErrorBoundary.defaultProps = {
|
|
|
|
errorClasses: '',
|
|
|
|
message: '',
|
2018-10-09 22:38:14 +03:00
|
|
|
children: null,
|
2018-08-31 20:53:45 +03:00
|
|
|
};
|