import PropTypes from 'prop-types'; import React from 'react'; import { FormattedMessage } from 'react-intl'; import Icon from 'soapbox/components/icon'; import { captureException } from 'soapbox/monitoring'; import sourceCode from 'soapbox/utils/code'; export default class ErrorBoundary extends React.PureComponent { static propTypes = { children: PropTypes.node, }; state = { hasError: false, componentStack: undefined, } componentDidCatch(error, info) { captureException(error); this.setState({ hasError: true, error, componentStack: info && info.componentStack, }); import(/* webpackChunkName: "error" */'bowser') .then(({ default: Bowser }) => { this.setState({ browser: Bowser.getParser(window.navigator.userAgent), }); }) .catch(() => {}); } 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 => { localStorage.clear(); sessionStorage.clear(); } render() { const { browser, hasError } = this.state; if (!hasError) { return this.props.children; } const errorText = this.getErrorText(); return (
); } }