2019-11-15 14:15:55 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import PropTypes from 'prop-types';
|
2020-12-14 11:54:34 +00:00
|
|
|
import { Component } from 'react';
|
2019-11-15 14:15:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import BlockError from './block-error';
|
|
|
|
import './style.scss';
|
|
|
|
|
|
|
|
class BlockErrorBoundary extends Component {
|
2020-04-15 15:15:56 +00:00
|
|
|
state = { errorMessage: '', hasError: false };
|
2019-11-15 14:15:55 +00:00
|
|
|
|
|
|
|
static getDerivedStateFromError( error ) {
|
2020-01-31 13:01:34 +00:00
|
|
|
if (
|
|
|
|
typeof error.statusText !== 'undefined' &&
|
|
|
|
typeof error.status !== 'undefined'
|
|
|
|
) {
|
|
|
|
return {
|
|
|
|
errorMessage: (
|
2020-12-14 11:54:34 +00:00
|
|
|
<>
|
2020-01-31 13:01:34 +00:00
|
|
|
<strong>{ error.status }</strong>:
|
|
|
|
{ error.statusText }
|
2020-12-14 11:54:34 +00:00
|
|
|
</>
|
2020-01-31 13:01:34 +00:00
|
|
|
),
|
|
|
|
hasError: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-11-15 14:15:55 +00:00
|
|
|
return { errorMessage: error.message, hasError: true };
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2020-03-06 11:43:40 +00:00
|
|
|
const {
|
|
|
|
header,
|
|
|
|
imageUrl,
|
|
|
|
showErrorMessage,
|
|
|
|
text,
|
|
|
|
errorMessagePrefix,
|
2020-12-04 09:52:52 +00:00
|
|
|
renderError,
|
2020-03-06 11:43:40 +00:00
|
|
|
} = this.props;
|
2019-11-15 14:15:55 +00:00
|
|
|
const { errorMessage, hasError } = this.state;
|
|
|
|
|
|
|
|
if ( hasError ) {
|
2020-12-04 09:52:52 +00:00
|
|
|
if ( typeof renderError === 'function' ) {
|
|
|
|
return renderError( { errorMessage } );
|
|
|
|
}
|
2019-11-15 14:15:55 +00:00
|
|
|
return (
|
|
|
|
<BlockError
|
|
|
|
errorMessage={ showErrorMessage ? errorMessage : null }
|
|
|
|
header={ header }
|
|
|
|
imageUrl={ imageUrl }
|
|
|
|
text={ text }
|
2020-03-06 11:43:40 +00:00
|
|
|
errorMessagePrefix={ errorMessagePrefix }
|
2019-11-15 14:15:55 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.props.children;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BlockErrorBoundary.propTypes = {
|
|
|
|
/**
|
|
|
|
* Text to display as the heading of the error block.
|
|
|
|
* If it's `null` or an empty string, no header will be displayed.
|
|
|
|
* If it's not defined, the default header will be used.
|
|
|
|
*/
|
|
|
|
header: PropTypes.string,
|
|
|
|
/**
|
|
|
|
* URL of the image to display.
|
|
|
|
* If it's `null` or an empty string, no image will be displayed.
|
|
|
|
* If it's not defined, the default image will be used.
|
|
|
|
*/
|
|
|
|
imageUrl: PropTypes.string,
|
|
|
|
/**
|
|
|
|
* Whether to display the JS error message.
|
|
|
|
*/
|
|
|
|
showErrorMessage: PropTypes.bool,
|
|
|
|
/**
|
|
|
|
* Text to display in the error block below the header.
|
|
|
|
* If it's `null` or an empty string, nothing will be displayed.
|
|
|
|
* If it's not defined, the default text will be used.
|
|
|
|
*/
|
2020-03-06 11:43:40 +00:00
|
|
|
text: PropTypes.node,
|
|
|
|
/**
|
|
|
|
* Text preceeding the error message.
|
|
|
|
*/
|
|
|
|
errorMessagePrefix: PropTypes.string,
|
2020-12-04 09:52:52 +00:00
|
|
|
/**
|
|
|
|
* Render function to show a custom error component.
|
|
|
|
*/
|
|
|
|
renderError: PropTypes.func,
|
2019-11-15 14:15:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
BlockErrorBoundary.defaultProps = {
|
2020-01-31 13:01:34 +00:00
|
|
|
showErrorMessage: true,
|
2019-11-15 14:15:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default BlockErrorBoundary;
|