errorBoundary.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import {Component} from 'react';
  2. import {browserHistory} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import * as Sentry from '@sentry/react';
  5. import Alert from 'sentry/components/alert';
  6. import DetailedError from 'sentry/components/errors/detailedError';
  7. import {t} from 'sentry/locale';
  8. import getDynamicText from 'sentry/utils/getDynamicText';
  9. type DefaultProps = {
  10. mini: boolean;
  11. };
  12. type Props = DefaultProps & {
  13. // To add context for better UX
  14. className?: string;
  15. customComponent?: React.ReactNode;
  16. // To add context for better error reporting
  17. errorTag?: Record<string, string>;
  18. message?: React.ReactNode;
  19. };
  20. type State = {
  21. error: Error | null;
  22. };
  23. const exclamation = ['Raspberries', 'Snap', 'Frig', 'Welp', 'Uhhhh', 'Hmmm'] as const;
  24. function getExclamation() {
  25. return exclamation[Math.floor(Math.random() * exclamation.length)];
  26. }
  27. class ErrorBoundary extends Component<Props, State> {
  28. static defaultProps: DefaultProps = {
  29. mini: false,
  30. };
  31. state: State = {
  32. error: null,
  33. };
  34. componentDidMount() {
  35. this._isMounted = true;
  36. // Listen for route changes so we can clear error
  37. this.unlistenBrowserHistory = browserHistory.listen(() => {
  38. // Prevent race between component unmount and browserHistory change
  39. // Setting state on a component that is being unmounted throws an error
  40. if (this._isMounted) {
  41. this.setState({error: null});
  42. }
  43. });
  44. }
  45. componentDidCatch(error: Error, errorInfo: React.ErrorInfo) {
  46. const {errorTag} = this.props;
  47. this.setState({error});
  48. Sentry.withScope(scope => {
  49. if (errorTag) {
  50. Object.keys(errorTag).forEach(tag => scope.setTag(tag, errorTag[tag]));
  51. }
  52. // Based on https://github.com/getsentry/sentry-javascript/blob/6f4ad562c469f546f1098136b65583309d03487b/packages/react/src/errorboundary.tsx#L75-L85
  53. const errorBoundaryError = new Error(error.message);
  54. errorBoundaryError.name = `React ErrorBoundary ${errorBoundaryError.name}`;
  55. errorBoundaryError.stack = errorInfo.componentStack;
  56. error.cause = errorBoundaryError;
  57. scope.setExtra('errorInfo', errorInfo);
  58. Sentry.captureException(error);
  59. });
  60. }
  61. componentWillUnmount() {
  62. this._isMounted = false;
  63. if (this.unlistenBrowserHistory) {
  64. this.unlistenBrowserHistory();
  65. }
  66. }
  67. private unlistenBrowserHistory?: ReturnType<typeof browserHistory.listen>;
  68. private _isMounted = false;
  69. render() {
  70. const {error} = this.state;
  71. if (!error) {
  72. // when there's not an error, render children untouched
  73. return this.props.children;
  74. }
  75. const {customComponent, mini, message, className} = this.props;
  76. if (typeof customComponent !== 'undefined') {
  77. return customComponent;
  78. }
  79. if (mini) {
  80. return (
  81. <Alert type="error" showIcon className={className}>
  82. {message || t('There was a problem rendering this component')}
  83. </Alert>
  84. );
  85. }
  86. return (
  87. <Wrapper data-test-id="error-boundary">
  88. <DetailedError
  89. heading={getDynamicText({
  90. value: getExclamation(),
  91. fixed: exclamation[0],
  92. })}
  93. message={t(
  94. `Something went horribly wrong rendering this page.
  95. We use a decent error reporting service so this will probably be fixed soon. Unless our error reporting service is also broken. That would be awkward.
  96. Anyway, we apologize for the inconvenience.`
  97. )}
  98. />
  99. <StackTrace>{error.toString()}</StackTrace>
  100. </Wrapper>
  101. );
  102. }
  103. }
  104. const Wrapper = styled('div')`
  105. color: ${p => p.theme.textColor};
  106. padding: ${p => p.theme.grid * 3}px;
  107. max-width: 1000px;
  108. margin: auto;
  109. `;
  110. const StackTrace = styled('pre')`
  111. white-space: pre-wrap;
  112. margin: 32px;
  113. margin-left: 85px;
  114. margin-right: 18px;
  115. `;
  116. export default ErrorBoundary;