error-reporter.ts 5.9 KB

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