routeError.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import {useEffect} from 'react';
  2. // eslint-disable-next-line no-restricted-imports
  3. import {withRouter, WithRouterProps} from 'react-router';
  4. import styled from '@emotion/styled';
  5. import * as Sentry from '@sentry/react';
  6. import Alert from 'sentry/components/alert';
  7. import List from 'sentry/components/list';
  8. import ListItem from 'sentry/components/list/listItem';
  9. import {t, tct} from 'sentry/locale';
  10. import OrganizationStore from 'sentry/stores/organizationStore';
  11. import {useLegacyStore} from 'sentry/stores/useLegacyStore';
  12. import space from 'sentry/styles/space';
  13. import {Project} from 'sentry/types';
  14. import getRouteStringFromRoutes from 'sentry/utils/getRouteStringFromRoutes';
  15. import withProject from 'sentry/utils/withProject';
  16. type Props = WithRouterProps & {
  17. /**
  18. * Disable logging to Sentry
  19. */
  20. disableLogSentry?: boolean;
  21. /**
  22. * Disable the report dialog
  23. */
  24. disableReport?: boolean;
  25. error?: Error;
  26. project?: Project;
  27. };
  28. function RouteError({error, disableLogSentry, disableReport, project, routes}: Props) {
  29. const {organization} = useLegacyStore(OrganizationStore);
  30. useEffect(() => {
  31. if (disableLogSentry) {
  32. return undefined;
  33. }
  34. if (!error) {
  35. return undefined;
  36. }
  37. const route = getRouteStringFromRoutes(routes);
  38. const enrichScopeContext = (scope: Sentry.Scope) => {
  39. scope.setExtra('route', route);
  40. scope.setExtra('orgFeatures', organization?.features ?? []);
  41. scope.setExtra('orgAccess', organization?.access ?? []);
  42. scope.setExtra('projectFeatures', project?.features ?? []);
  43. return scope;
  44. };
  45. if (route) {
  46. // Unexpectedly, error.message would sometimes not have a setter
  47. // property, causing another exception to be thrown, and losing the
  48. // original error in the process. Wrapping the mutation in a try-catch in
  49. // an attempt to preserve the original error for logging.
  50. //
  51. // See https://github.com/getsentry/sentry/issues/16314 for more details.
  52. try {
  53. error.message = `${error.message}: ${route}`;
  54. } catch (e) {
  55. Sentry.withScope(scope => {
  56. enrichScopeContext(scope);
  57. Sentry.captureException(e);
  58. });
  59. }
  60. }
  61. // TODO(dcramer): show something in addition to embed (that contains it?)
  62. // throw this in a timeout so if it errors we don't fall over
  63. const reportDialogTimeout = window.setTimeout(() => {
  64. Sentry.withScope(scope => {
  65. enrichScopeContext(scope);
  66. Sentry.captureException(error);
  67. });
  68. if (!disableReport) {
  69. Sentry.showReportDialog();
  70. }
  71. });
  72. return function cleanup() {
  73. window.clearTimeout(reportDialogTimeout);
  74. };
  75. // eslint-disable-next-line react-hooks/exhaustive-deps
  76. }, [error, disableLogSentry]);
  77. // Remove the report dialog on unmount
  78. useEffect(() => () => document.querySelector('.sentry-error-embed-wrapper')?.remove());
  79. // TODO(dcramer): show additional resource links
  80. return (
  81. <Alert type="error">
  82. <Heading>{t('Oops! Something went wrong')}</Heading>
  83. <p>
  84. {t(`
  85. It looks like you've hit an issue in our client application. Don't worry though!
  86. We use Sentry to monitor Sentry and it's likely we're already looking into this!
  87. `)}
  88. </p>
  89. <p>{t("If you're daring, you may want to try the following:")}</p>
  90. <List symbol="bullet">
  91. {window && window.adblockSuspected && (
  92. <ListItem>
  93. {t(
  94. "We detected something AdBlock-like. Try disabling it, as it's known to cause issues."
  95. )}
  96. </ListItem>
  97. )}
  98. <ListItem>
  99. {tct(`Give it a few seconds and [link:reload the page].`, {
  100. link: (
  101. <a
  102. onClick={() => {
  103. window.location.href = window.location.href;
  104. }}
  105. />
  106. ),
  107. })}
  108. </ListItem>
  109. <ListItem>
  110. {tct(`If all else fails, [link:contact us] with more details.`, {
  111. link: <a href="https://github.com/getsentry/sentry/issues/new/choose" />,
  112. })}
  113. </ListItem>
  114. </List>
  115. </Alert>
  116. );
  117. }
  118. const Heading = styled('h1')`
  119. font-size: ${p => p.theme.fontSizeLarge};
  120. line-height: 1.4;
  121. margin-bottom: ${space(1)};
  122. `;
  123. export default withRouter(withProject(RouteError));