routeError.tsx 4.3 KB

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