error-reporter.js 3.5 KB

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