error-reporter.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. ],
  43. denyUrls: [
  44. // Chrome extensions
  45. /extensions\//i,
  46. /^chrome:\/\//i,
  47. ],
  48. beforeSend(event) {
  49. // Limit number of events sent to Sentry to 100 events "per page load",
  50. // (i.e. the cap will be reset if the page is reloaded). This prevent
  51. // hitting their server-side event cap.
  52. eventCount++
  53. if (eventCount > 100) {
  54. return null // Block the event from sending
  55. }
  56. // Do not send events related to third party code (extensions)
  57. if (
  58. event.extra?.arguments?.[0]?.type === 'UNSTABLE_editor:extensions'
  59. ) {
  60. return null // Block the event from sending
  61. }
  62. return event
  63. },
  64. })
  65. Sentry.setUser({ id: getMeta('ol-user_id') })
  66. const splitTestAssignments = getMeta('ol-splitTestVariants')
  67. if (splitTestAssignments) {
  68. for (const [name, value] of Object.entries(splitTestAssignments)) {
  69. // Ensure Sentry tag name is within the 32-character limit
  70. Sentry.setTag(`ol.${name}`.slice(0, 32), value.toString())
  71. }
  72. }
  73. return Sentry
  74. })
  75. // If Sentry fails to load, use the null reporter instead
  76. .catch(error => {
  77. debugConsole.error(error)
  78. return nullReporter()
  79. })
  80. )
  81. }
  82. function nullReporter() {
  83. return Promise.resolve({
  84. captureException: debugConsole.error,
  85. captureMessage: debugConsole.error,
  86. })
  87. }
  88. export function captureException(err, options) {
  89. options = options || {}
  90. const extra = Object.assign(OError.getFullInfo(err), options.extra || {})
  91. const fullStack = OError.getFullStack(err)
  92. if (err.stack !== fullStack) {
  93. // Attach tracebacks from OError.tag() and OError.cause.
  94. extra.fullStack = fullStack
  95. }
  96. reporterPromise.then(reporter =>
  97. reporter.captureException(err, {
  98. ...options,
  99. extra,
  100. })
  101. )
  102. }
  103. export function captureMessage(...args) {
  104. reporterPromise.then(reporter => reporter.captureMessage(...args))
  105. }