error-reporter.js 3.9 KB

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