import PropTypes from 'prop-types'; import { PureComponent } from 'react'; import { FormattedMessage } from 'react-intl'; import { Helmet } from 'react-helmet'; import StackTrace from 'stacktrace-js'; import { version, source_url } from 'mastodon/initial_state'; export default class ErrorBoundary extends PureComponent { static propTypes = { children: PropTypes.node, }; state = { hasError: false, errorMessage: undefined, stackTrace: undefined, mappedStackTrace: undefined, componentStack: undefined, }; componentDidCatch (error, info) { this.setState({ hasError: true, errorMessage: error.toString(), stackTrace: error.stack, componentStack: info && info.componentStack, mappedStackTrace: undefined, }); StackTrace.fromError(error).then((stackframes) => { this.setState({ mappedStackTrace: stackframes.map((sf) => sf.toString()).join('\n'), }); }).catch(() => { this.setState({ mappedStackTrace: undefined, }); }); } handleCopyStackTrace = () => { const { errorMessage, stackTrace, mappedStackTrace } = this.state; const textarea = document.createElement('textarea'); let contents = [errorMessage, stackTrace]; if (mappedStackTrace) { contents.push(mappedStackTrace); } textarea.textContent = contents.join('\n\n\n'); textarea.style.position = 'fixed'; document.body.appendChild(textarea); try { textarea.select(); document.execCommand('copy'); } catch (e) { } finally { document.body.removeChild(textarea); } this.setState({ copied: true }); setTimeout(() => this.setState({ copied: false }), 700); }; render() { const { hasError, copied, errorMessage } = this.state; if (!hasError) { return this.props.children; } const likelyBrowserAddonIssue = errorMessage && errorMessage.includes('NotFoundError'); return (

{ likelyBrowserAddonIssue ? ( ) : ( )}

{ likelyBrowserAddonIssue ? ( ) : ( )}

Mastodon v{version} · ·

); } }