Merge branch 'error-text' into 'develop'

ErrorBoundary: display error text, fixes #672

Closes #672

See merge request soapbox-pub/soapbox-fe!581
This commit is contained in:
Alex Gleason 2021-07-05 23:25:29 +00:00
commit d7247c902b
2 changed files with 38 additions and 4 deletions

View File

@ -10,18 +10,35 @@ export default class ErrorBoundary extends React.PureComponent {
state = { state = {
hasError: false, hasError: false,
stackTrace: undefined,
componentStack: undefined, componentStack: undefined,
} }
componentDidCatch(error, info) { componentDidCatch(error, info) {
this.setState({ this.setState({
hasError: true, hasError: true,
stackTrace: error.stack, error,
componentStack: info && info.componentStack, componentStack: info && info.componentStack,
}); });
} }
setTextareaRef = c => {
this.textarea = c;
}
handleCopy = e => {
if (!this.textarea) return;
this.textarea.select();
this.textarea.setSelectionRange(0, 99999);
document.execCommand('copy');
}
getErrorText = () => {
const { error, componentStack } = this.state;
return error + componentStack;
}
clearCookies = e => { clearCookies = e => {
localStorage.clear(); localStorage.clear();
sessionStorage.clear(); sessionStorage.clear();
@ -34,6 +51,8 @@ export default class ErrorBoundary extends React.PureComponent {
return this.props.children; return this.props.children;
} }
const errorText = this.getErrorText();
return ( return (
<div className='error-boundary'> <div className='error-boundary'>
<div> <div>
@ -43,6 +62,13 @@ export default class ErrorBoundary extends React.PureComponent {
<i className='fa fa-reply' aria-hidden='true' />&nbsp; <i className='fa fa-reply' aria-hidden='true' />&nbsp;
<FormattedMessage id='alert.unexpected.return_home' defaultMessage='Return Home' /> <FormattedMessage id='alert.unexpected.return_home' defaultMessage='Return Home' />
</a> </a>
{errorText && <textarea
ref={this.setTextareaRef}
className='error-boundary__component-stack'
value={errorText}
onClick={this.handleCopy}
readOnly
/>}
<p className='help-text'> <p className='help-text'>
<FormattedMessage <FormattedMessage
id='alert.unexpected.help_text' id='alert.unexpected.help_text'

View File

@ -9,7 +9,7 @@
display: block; display: block;
text-align: center; text-align: center;
font-size: 70px; font-size: 70px;
margin-bottom: 20px; margin: 20px 0;
opacity: 0.5; opacity: 0.5;
} }
@ -38,8 +38,16 @@
font-style: italic; font-style: italic;
font-size: 14px; font-size: 14px;
padding: 20px 10px 0; padding: 20px 10px 0;
margin-top: 70px; margin-top: 20px;
opacity: 0.7; opacity: 0.7;
border-top: 1px solid; border-top: 1px solid;
} }
&__component-stack {
width: 100%;
height: 200px;
text-align: left;
margin: 20px 0;
padding: 10px;
}
} }