routeError.tsx 4.2 KB

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