2021-12-03 17:23:25 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { Component } from 'react';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import BlockError from './block-error';
|
|
|
|
import './style.scss';
|
|
|
|
|
|
|
|
import type {
|
|
|
|
DerivedStateReturn,
|
|
|
|
ReactError,
|
|
|
|
BlockErrorBoundaryProps,
|
|
|
|
} from './types';
|
|
|
|
|
|
|
|
class BlockErrorBoundary extends Component< BlockErrorBoundaryProps > {
|
|
|
|
state = { errorMessage: '', hasError: false };
|
|
|
|
|
|
|
|
static getDerivedStateFromError( error: ReactError ): DerivedStateReturn {
|
|
|
|
if (
|
|
|
|
typeof error.statusText !== 'undefined' &&
|
|
|
|
typeof error.status !== 'undefined'
|
|
|
|
) {
|
|
|
|
return {
|
|
|
|
errorMessage: (
|
|
|
|
<>
|
|
|
|
<strong>{ error.status }</strong>:
|
|
|
|
{ error.statusText }
|
|
|
|
</>
|
|
|
|
),
|
|
|
|
hasError: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return { errorMessage: error.message, hasError: true };
|
|
|
|
}
|
|
|
|
|
|
|
|
render(): JSX.Element | React.ReactNode {
|
|
|
|
const {
|
|
|
|
header,
|
|
|
|
imageUrl,
|
|
|
|
showErrorMessage = true,
|
2022-01-25 12:01:19 +00:00
|
|
|
showErrorBlock = true,
|
2021-12-03 17:23:25 +00:00
|
|
|
text,
|
|
|
|
errorMessagePrefix,
|
|
|
|
renderError,
|
|
|
|
button,
|
|
|
|
} = this.props;
|
|
|
|
const { errorMessage, hasError } = this.state;
|
|
|
|
|
|
|
|
if ( hasError ) {
|
|
|
|
if ( typeof renderError === 'function' ) {
|
|
|
|
return renderError( { errorMessage } );
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<BlockError
|
2022-01-25 12:01:19 +00:00
|
|
|
showErrorBlock={ showErrorBlock }
|
2021-12-03 17:23:25 +00:00
|
|
|
errorMessage={ showErrorMessage ? errorMessage : null }
|
|
|
|
header={ header }
|
|
|
|
imageUrl={ imageUrl }
|
|
|
|
text={ text }
|
|
|
|
errorMessagePrefix={ errorMessagePrefix }
|
|
|
|
button={ button }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.props.children;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default BlockErrorBoundary;
|