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.

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