2018-11-28 14:01:40 +00:00
import PropTypes from 'prop-types' ;
2023-05-28 14:38:10 +00:00
import { PureComponent } from 'react' ;
2018-11-28 14:01:40 +00:00
import { FormattedMessage } from 'react-intl' ;
2023-05-28 14:38:10 +00:00
import { Helmet } from 'react-helmet' ;
import StackTrace from 'stacktrace-js' ;
2023-07-27 17:25:07 +00:00
import { version , source _url } from 'flavours/glitch/initial_state' ;
2018-11-28 14:01:40 +00:00
2023-05-28 12:18:23 +00:00
export default class ErrorBoundary extends PureComponent {
2018-11-28 14:01:40 +00:00
static propTypes = {
children : PropTypes . node ,
} ;
state = {
hasError : false ,
2020-02-19 22:16:29 +00:00
errorMessage : undefined ,
2018-11-28 14:01:40 +00:00
stackTrace : undefined ,
2020-02-19 22:16:29 +00:00
mappedStackTrace : undefined ,
2018-11-28 14:01:40 +00:00
componentStack : undefined ,
2023-02-03 19:52:07 +00:00
} ;
2018-11-28 14:01:40 +00:00
2023-07-27 17:25:07 +00:00
componentDidCatch ( error , info ) {
2018-11-28 14:01:40 +00:00
this . setState ( {
hasError : true ,
2020-02-19 22:16:29 +00:00
errorMessage : error . toString ( ) ,
2018-11-28 14:01:40 +00:00
stackTrace : error . stack ,
componentStack : info && info . componentStack ,
2020-02-19 22:16:29 +00:00
mappedStackTrace : undefined ,
} ) ;
StackTrace . fromError ( error ) . then ( ( stackframes ) => {
this . setState ( {
mappedStackTrace : stackframes . map ( ( sf ) => sf . toString ( ) ) . join ( '\n' ) ,
} ) ;
} ) . catch ( ( ) => {
this . setState ( {
mappedStackTrace : undefined ,
} ) ;
2018-11-28 14:01:40 +00:00
} ) ;
}
2023-07-27 17:25:07 +00:00
handleCopyStackTrace = ( ) => {
const { errorMessage , stackTrace , mappedStackTrace } = this . state ;
const textarea = document . createElement ( 'textarea' ) ;
2018-11-28 14:01:40 +00:00
2023-07-27 17:25:07 +00:00
let contents = [ errorMessage , stackTrace ] ;
if ( mappedStackTrace ) {
contents . push ( mappedStackTrace ) ;
}
2018-11-28 14:01:40 +00:00
2023-07-27 17:25:07 +00:00
textarea . textContent = contents . join ( '\n\n\n' ) ;
textarea . style . position = 'fixed' ;
2018-11-28 14:01:40 +00:00
2023-07-27 17:25:07 +00:00
document . body . appendChild ( textarea ) ;
2020-09-14 13:05:22 +00:00
2023-07-27 17:25:07 +00:00
try {
textarea . select ( ) ;
document . execCommand ( 'copy' ) ;
} catch ( e ) {
} finally {
document . body . removeChild ( textarea ) ;
2018-11-28 14:01:40 +00:00
}
2023-07-27 17:25:07 +00:00
this . setState ( { copied : true } ) ;
setTimeout ( ( ) => this . setState ( { copied : false } ) , 700 ) ;
} ;
render ( ) {
const { hasError , copied , errorMessage } = this . state ;
if ( ! hasError ) {
return this . props . children ;
2021-09-08 11:47:48 +00:00
}
2023-07-27 17:25:07 +00:00
const likelyBrowserAddonIssue = errorMessage && errorMessage . includes ( 'NotFoundError' ) ;
2018-11-28 14:01:40 +00:00
return (
2023-07-27 17:25:07 +00:00
< div className = 'error-boundary' >
< div >
< p className = 'error-boundary__error' >
{ likelyBrowserAddonIssue ? (
< FormattedMessage id = 'error.unexpected_crash.explanation_addons' defaultMessage = 'This page could not be displayed correctly. This error is likely caused by a browser add-on or automatic translation tools.' / >
) : (
< FormattedMessage id = 'error.unexpected_crash.explanation' defaultMessage = 'Due to a bug in our code or a browser compatibility issue, this page could not be displayed correctly.' / >
) }
2019-07-01 11:38:00 +00:00
< / p >
2023-07-27 17:25:07 +00:00
< p >
{ likelyBrowserAddonIssue ? (
< FormattedMessage id = 'error.unexpected_crash.next_steps_addons' defaultMessage = 'Try disabling them and refreshing the page. If that does not help, you may still be able to use Mastodon through a different browser or native app.' / >
) : (
< FormattedMessage id = 'error.unexpected_crash.next_steps' defaultMessage = 'Try refreshing the page. If that does not help, you may still be able to use Mastodon through a different browser or native app.' / >
2019-07-01 11:38:00 +00:00
) }
2023-07-27 17:25:07 +00:00
< / p >
< p className = 'error-boundary__footer' > Mastodon v { version } · < a href = { source _url } rel = 'noopener noreferrer' target = '_blank' > < FormattedMessage id = 'errors.unexpected_crash.report_issue' defaultMessage = 'Report issue' / > < / a > · < button onClick = { this . handleCopyStackTrace } className = { copied ? 'copied' : '' } > < FormattedMessage id = 'errors.unexpected_crash.copy_stacktrace' defaultMessage = 'Copy stacktrace to clipboard' / > < / button > < / p >
2018-11-28 14:01:40 +00:00
< / div >
2022-10-20 12:35:29 +00:00
< Helmet >
< meta name = 'robots' content = 'noindex' / >
< / Helmet >
2018-11-28 14:01:40 +00:00
< / div >
) ;
}
}