error-reporter.js 4.0 KB

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