errorBoundary.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. scope.setExtra('errorInfo', errorInfo);
  53. Sentry.captureException(error);
  54. });
  55. }
  56. componentWillUnmount() {
  57. this._isMounted = false;
  58. if (this.unlistenBrowserHistory) {
  59. this.unlistenBrowserHistory();
  60. }
  61. }
  62. private unlistenBrowserHistory?: ReturnType<typeof browserHistory.listen>;
  63. private _isMounted = false;
  64. render() {
  65. const {error} = this.state;
  66. if (!error) {
  67. // when there's not an error, render children untouched
  68. return this.props.children;
  69. }
  70. const {customComponent, mini, message, className} = this.props;
  71. if (typeof customComponent !== 'undefined') {
  72. return customComponent;
  73. }
  74. if (mini) {
  75. return (
  76. <Alert type="error" showIcon className={className}>
  77. {message || t('There was a problem rendering this component')}
  78. </Alert>
  79. );
  80. }
  81. return (
  82. <Wrapper>
  83. <DetailedError
  84. heading={getDynamicText({
  85. value: getExclamation(),
  86. fixed: exclamation[0],
  87. })}
  88. message={t(
  89. `Something went horribly wrong rendering this page.
  90. 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.
  91. Anyway, we apologize for the inconvenience.`
  92. )}
  93. />
  94. <StackTrace>{error.toString()}</StackTrace>
  95. </Wrapper>
  96. );
  97. }
  98. }
  99. const Wrapper = styled('div')`
  100. color: ${p => p.theme.textColor};
  101. padding: ${p => p.theme.grid * 3}px;
  102. max-width: 1000px;
  103. margin: auto;
  104. `;
  105. const StackTrace = styled('pre')`
  106. white-space: pre-wrap;
  107. margin: 32px;
  108. margin-left: 85px;
  109. margin-right: 18px;
  110. `;
  111. export default ErrorBoundary;