error-reporter.ts 4.5 KB

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