errorBoundary.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import * as React 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 'app/components/alert';
  6. import DetailedError from 'app/components/errors/detailedError';
  7. import {IconFlag} from 'app/icons';
  8. import {t} from 'app/locale';
  9. import getDynamicText from 'app/utils/getDynamicText';
  10. type DefaultProps = {
  11. mini: boolean;
  12. };
  13. type Props = DefaultProps & {
  14. // To add context for better UX
  15. className?: string;
  16. customComponent?: React.ReactNode;
  17. message?: React.ReactNode;
  18. // To add context for better error reporting
  19. errorTag?: Record<string, string>;
  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 React.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. scope.setExtra('errorInfo', errorInfo);
  54. Sentry.captureException(error);
  55. });
  56. }
  57. componentWillUnmount() {
  58. this._isMounted = false;
  59. if (this.unlistenBrowserHistory) {
  60. this.unlistenBrowserHistory();
  61. }
  62. }
  63. private unlistenBrowserHistory?: ReturnType<typeof browserHistory.listen>;
  64. private _isMounted = false;
  65. render() {
  66. const {error} = this.state;
  67. if (!error) {
  68. // when there's not an error, render children untouched
  69. return this.props.children;
  70. }
  71. const {customComponent, mini, message, className} = this.props;
  72. if (typeof customComponent !== 'undefined') {
  73. return customComponent;
  74. }
  75. if (mini) {
  76. return (
  77. <Alert type="error" icon={<IconFlag size="md" />} className={className}>
  78. {message || t('There was a problem rendering this component')}
  79. </Alert>
  80. );
  81. }
  82. return (
  83. <Wrapper>
  84. <DetailedError
  85. heading={getDynamicText({
  86. value: getExclamation(),
  87. fixed: exclamation[0],
  88. })}
  89. message={t(
  90. `Something went horribly wrong rendering this page.
  91. 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.
  92. Anyway, we apologize for the inconvenience.`
  93. )}
  94. />
  95. <StackTrace>{error.toString()}</StackTrace>
  96. </Wrapper>
  97. );
  98. }
  99. }
  100. const Wrapper = styled('div')`
  101. color: ${p => p.theme.textColor};
  102. padding: ${p => p.theme.grid * 3}px;
  103. max-width: 1000px;
  104. margin: auto;
  105. `;
  106. const StackTrace = styled('pre')`
  107. white-space: pre-wrap;
  108. margin: 32px;
  109. margin-left: 85px;
  110. margin-right: 18px;
  111. `;
  112. export default ErrorBoundary;