errorBoundary.tsx 3.8 KB

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