2019-03-15 04:35:45 +00:00
import PropTypes from 'prop-types' ;
2023-05-23 16:15:17 +01:00
import { PureComponent } from 'react' ;
2019-10-01 19:48:49 +01:00
import { FormattedMessage } from 'react-intl' ;
2023-05-23 16:15:17 +01:00
2022-10-20 13:35:29 +01:00
import { Helmet } from 'react-helmet' ;
2019-03-15 04:35:45 +00:00
2023-05-23 16:15:17 +01:00
import StackTrace from 'stacktrace-js' ;
import { version , source _url } from 'mastodon/initial_state' ;
2023-05-23 09:52:27 +01:00
export default class ErrorBoundary extends PureComponent {
2019-03-15 04:35:45 +00:00
static propTypes = {
children : PropTypes . node ,
} ;
state = {
hasError : false ,
2020-02-19 21:36:52 +00:00
errorMessage : undefined ,
2019-03-15 04:35:45 +00:00
stackTrace : undefined ,
2020-02-19 21:36:52 +00:00
mappedStackTrace : undefined ,
2019-03-15 04:35:45 +00:00
componentStack : undefined ,
2019-10-01 19:48:49 +01:00
} ;
2019-03-15 04:35:45 +00:00
2019-10-01 19:48:49 +01:00
componentDidCatch ( error , info ) {
2019-03-15 04:35:45 +00:00
this . setState ( {
hasError : true ,
2020-02-19 21:36:52 +00:00
errorMessage : error . toString ( ) ,
2019-03-15 04:35:45 +00:00
stackTrace : error . stack ,
componentStack : info && info . componentStack ,
2020-02-19 21:36:52 +00:00
mappedStackTrace : undefined ,
} ) ;
StackTrace . fromError ( error ) . then ( ( stackframes ) => {
this . setState ( {
mappedStackTrace : stackframes . map ( ( sf ) => sf . toString ( ) ) . join ( '\n' ) ,
} ) ;
} ) . catch ( ( ) => {
this . setState ( {
mappedStackTrace : undefined ,
} ) ;
2019-03-15 04:35:45 +00:00
} ) ;
}
2019-10-01 19:48:49 +01:00
handleCopyStackTrace = ( ) => {
2020-02-19 21:36:52 +00:00
const { errorMessage , stackTrace , mappedStackTrace } = this . state ;
2019-10-01 19:48:49 +01:00
const textarea = document . createElement ( 'textarea' ) ;
2020-02-19 21:36:52 +00:00
let contents = [ errorMessage , stackTrace ] ;
if ( mappedStackTrace ) {
contents . push ( mappedStackTrace ) ;
}
textarea . textContent = contents . join ( '\n\n\n' ) ;
2019-10-01 19:48:49 +01:00
textarea . style . position = 'fixed' ;
document . body . appendChild ( textarea ) ;
try {
textarea . select ( ) ;
document . execCommand ( 'copy' ) ;
2024-09-11 14:59:46 +01:00
} catch {
// do nothing
2019-10-01 19:48:49 +01:00
} finally {
document . body . removeChild ( textarea ) ;
}
this . setState ( { copied : true } ) ;
setTimeout ( ( ) => this . setState ( { copied : false } ) , 700 ) ;
2023-01-30 00:45:35 +00:00
} ;
2019-10-01 19:48:49 +01:00
2019-03-15 04:35:45 +00:00
render ( ) {
2020-09-14 14:05:22 +01:00
const { hasError , copied , errorMessage } = this . state ;
2019-03-15 04:35:45 +00:00
if ( ! hasError ) {
return this . props . children ;
}
2020-09-14 14:05:22 +01:00
const likelyBrowserAddonIssue = errorMessage && errorMessage . includes ( 'NotFoundError' ) ;
2019-03-15 04:35:45 +00:00
return (
2019-10-01 19:48:49 +01:00
< div className = 'error-boundary' >
< div >
2020-09-14 14:05:22 +01:00
< 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.' / >
) }
< / p >
2022-10-20 13:35:29 +01:00
2020-09-14 14:05:22 +01: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.' / >
) }
< / p >
2022-10-20 13:35:29 +01:00
2020-01-23 21:00:13 +00:00
< 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 >
2019-10-01 19:48:49 +01:00
< / div >
2022-10-20 13:35:29 +01:00
< Helmet >
< meta name = 'robots' content = 'noindex' / >
< / Helmet >
2019-03-15 04:35:45 +00:00
< / div >
) ;
}
}