error-reporter.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Conditionally enable Sentry based on whether the DSN token is set
  2. import getMeta from '../utils/meta'
  3. import OError from '@overleaf/o-error'
  4. import { debugConsole } from '@/utils/debugging'
  5. import type { ErrorEvent } from '@sentry/types/types/event'
  6. const {
  7. sentryAllowedOriginRegex,
  8. sentryDsn,
  9. sentryEnvironment,
  10. sentryRelease,
  11. } = getMeta('ol-ExposedSettings')
  12. const reporterPromise = sentryDsn ? sentryReporter() : nullReporter()
  13. const isPropensityNetworkError = (err: ErrorEvent) => {
  14. const errorBreadcrumbs = err.breadcrumbs?.filter(b => b.level === 'error')
  15. if (!errorBreadcrumbs || errorBreadcrumbs.length !== 1) {
  16. // don't ignore Propensity if there are more errors to report
  17. return false
  18. }
  19. return Boolean(
  20. errorBreadcrumbs[0]?.data?.url?.startsWith(
  21. 'https://analytics.propensity.com/'
  22. )
  23. )
  24. }
  25. function sentryReporter() {
  26. return (
  27. import(/* webpackMode: "eager" */ '@sentry/browser')
  28. .then(Sentry => {
  29. let eventCount = 0
  30. Sentry.init({
  31. dsn: sentryDsn,
  32. environment: sentryEnvironment,
  33. release: sentryRelease,
  34. autoSessionTracking: false,
  35. // Ignore errors unless they come from our origins
  36. // Adapted from: https://docs.sentry.io/platforms/javascript/configuration/filtering/#decluttering-sentry
  37. allowUrls: [new RegExp(sentryAllowedOriginRegex)],
  38. ignoreErrors: [
  39. // Ignore very noisy error
  40. 'SecurityError: Permission denied to access property "pathname" on cross-origin object',
  41. // Ignore unhandled error that is "expected" - see https://github.com/overleaf/issues/issues/3321
  42. /^Missing PDF/,
  43. // Ignore "expected" error from aborted fetch - see https://github.com/overleaf/issues/issues/3321
  44. /^AbortError/,
  45. // Ignore spurious error from Ace internals - see https://github.com/overleaf/issues/issues/3321
  46. 'ResizeObserver loop limit exceeded',
  47. 'ResizeObserver loop completed with undelivered notifications.',
  48. // Microsoft Outlook SafeLink crawler
  49. // https://forum.sentry.io/t/unhandledrejection-non-error-promise-rejection-captured-with-value/14062
  50. /Non-Error promise rejection captured with value: Object Not Found Matching Id/,
  51. // Ignore CM6 error until upgraded
  52. "Cannot read properties of undefined (reading 'length')",
  53. // Ignore Angular digest iteration limit - see https://github.com/overleaf/internal/issues/15750
  54. '10 $digest() iterations reached',
  55. // Ignore a frequent unhandled promise rejection
  56. /Non-Error promise rejection captured with keys: currentTarget, detail, isTrusted, target/,
  57. /Non-Error promise rejection captured with keys: message, status/,
  58. ],
  59. denyUrls: [
  60. // Chrome extensions
  61. /extensions\//i,
  62. /^chrome:\/\//i,
  63. ],
  64. beforeSend(event) {
  65. // Limit number of events sent to Sentry to 100 events "per page load",
  66. // (i.e. the cap will be reset if the page is reloaded). This prevent
  67. // hitting their server-side event cap.
  68. eventCount++
  69. if (eventCount > 100) {
  70. return null // Block the event from sending
  71. }
  72. // Do not send events related to third party code (extensions)
  73. if (
  74. (event.extra?.arguments as { type: string }[] | undefined)?.[0]
  75. ?.type === 'UNSTABLE_editor:extensions'
  76. ) {
  77. return null // Block the event from sending
  78. }
  79. // Do not send link-sharing token to Sentry
  80. if (event.request?.headers?.Referer) {
  81. const refererUrl = new URL(event.request.headers.Referer)
  82. if (
  83. refererUrl.hostname === window.location.hostname &&
  84. refererUrl.pathname.startsWith('/read/')
  85. ) {
  86. refererUrl.pathname = '/read/'
  87. event.request.headers.Referer = refererUrl.toString()
  88. }
  89. }
  90. if (isPropensityNetworkError(event)) {
  91. return null
  92. }
  93. return event
  94. },
  95. })
  96. Sentry.setUser({ id: getMeta('ol-user_id') })
  97. const splitTestAssignments = getMeta('ol-splitTestVariants')
  98. if (splitTestAssignments) {
  99. for (const [name, value] of Object.entries(splitTestAssignments)) {
  100. // Ensure Sentry tag name is within the 32-character limit
  101. Sentry.setTag(`ol.${name}`.slice(0, 32), value.toString())
  102. }
  103. }
  104. return Sentry
  105. })
  106. // If Sentry fails to load, use the null reporter instead
  107. .catch(error => {
  108. debugConsole.error(error)
  109. return nullReporter()
  110. })
  111. )
  112. }
  113. function nullReporter() {
  114. return Promise.resolve({
  115. captureException: debugConsole.error,
  116. captureMessage: debugConsole.error,
  117. })
  118. }
  119. // https://develop.sentry.dev/sdk/data-model/event-payloads/contexts/
  120. // https://docs.sentry.io/platforms/javascript/enriching-events/context/#passing-context-directly
  121. type Options = {
  122. tags?: Record<string, any>
  123. extra?: Record<string, any>
  124. }
  125. export function captureException(err: Error, options?: Options) {
  126. options = options || {}
  127. const extra = Object.assign(OError.getFullInfo(err), options.extra || {})
  128. const fullStack = OError.getFullStack(err)
  129. if (err.stack !== fullStack) {
  130. // Attach tracebacks from OError.tag() and OError.cause.
  131. extra.fullStack = fullStack
  132. }
  133. reporterPromise.then(reporter =>
  134. reporter.captureException(err, {
  135. ...options,
  136. extra,
  137. })
  138. )
  139. }