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.

107 lines
3.6 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. import StackTrace from 'stacktrace-js';
  6. import { Helmet } from 'react-helmet';
  7. export default class ErrorBoundary extends React.PureComponent {
  8. static propTypes = {
  9. children: PropTypes.node,
  10. };
  11. state = {
  12. hasError: false,
  13. errorMessage: undefined,
  14. stackTrace: undefined,
  15. mappedStackTrace: undefined,
  16. componentStack: undefined,
  17. };
  18. componentDidCatch (error, info) {
  19. this.setState({
  20. hasError: true,
  21. errorMessage: error.toString(),
  22. stackTrace: error.stack,
  23. componentStack: info && info.componentStack,
  24. mappedStackTrace: undefined,
  25. });
  26. StackTrace.fromError(error).then((stackframes) => {
  27. this.setState({
  28. mappedStackTrace: stackframes.map((sf) => sf.toString()).join('\n'),
  29. });
  30. }).catch(() => {
  31. this.setState({
  32. mappedStackTrace: undefined,
  33. });
  34. });
  35. }
  36. handleCopyStackTrace = () => {
  37. const { errorMessage, stackTrace, mappedStackTrace } = this.state;
  38. const textarea = document.createElement('textarea');
  39. let contents = [errorMessage, stackTrace];
  40. if (mappedStackTrace) {
  41. contents.push(mappedStackTrace);
  42. }
  43. textarea.textContent = contents.join('\n\n\n');
  44. textarea.style.position = 'fixed';
  45. document.body.appendChild(textarea);
  46. try {
  47. textarea.select();
  48. document.execCommand('copy');
  49. } catch (e) {
  50. } finally {
  51. document.body.removeChild(textarea);
  52. }
  53. this.setState({ copied: true });
  54. setTimeout(() => this.setState({ copied: false }), 700);
  55. };
  56. render() {
  57. const { hasError, copied, errorMessage } = this.state;
  58. if (!hasError) {
  59. return this.props.children;
  60. }
  61. const likelyBrowserAddonIssue = errorMessage && errorMessage.includes('NotFoundError');
  62. return (
  63. <div className='error-boundary'>
  64. <div>
  65. <p className='error-boundary__error'>
  66. { likelyBrowserAddonIssue ? (
  67. <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.' />
  68. ) : (
  69. <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.' />
  70. )}
  71. </p>
  72. <p>
  73. { likelyBrowserAddonIssue ? (
  74. <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.' />
  75. ) : (
  76. <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.' />
  77. )}
  78. </p>
  79. <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>
  80. </div>
  81. <Helmet>
  82. <meta name='robots' content='noindex' />
  83. </Helmet>
  84. </div>
  85. );
  86. }
  87. }