You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
2.2 KiB

  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { FormattedMessage } from 'react-intl';
  4. import { version, source_url } from 'mastodon/initial_state';
  5. export default class ErrorBoundary extends React.PureComponent {
  6. static propTypes = {
  7. children: PropTypes.node,
  8. };
  9. state = {
  10. hasError: false,
  11. stackTrace: undefined,
  12. componentStack: undefined,
  13. };
  14. componentDidCatch (error, info) {
  15. this.setState({
  16. hasError: true,
  17. stackTrace: error.stack,
  18. componentStack: info && info.componentStack,
  19. copied: false,
  20. });
  21. }
  22. handleCopyStackTrace = () => {
  23. const { stackTrace } = this.state;
  24. const textarea = document.createElement('textarea');
  25. textarea.textContent = stackTrace;
  26. textarea.style.position = 'fixed';
  27. document.body.appendChild(textarea);
  28. try {
  29. textarea.select();
  30. document.execCommand('copy');
  31. } catch (e) {
  32. } finally {
  33. document.body.removeChild(textarea);
  34. }
  35. this.setState({ copied: true });
  36. setTimeout(() => this.setState({ copied: false }), 700);
  37. }
  38. render() {
  39. const { hasError, copied } = this.state;
  40. if (!hasError) {
  41. return this.props.children;
  42. }
  43. return (
  44. <div className='error-boundary'>
  45. <div>
  46. <p className='error-boundary__error'><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>
  47. <p><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>
  48. <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>
  49. </div>
  50. </div>
  51. );
  52. }
  53. }