error-reporter.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Conditionally enable Sentry based on whether the DSN token is set
  2. const reporterPromise = window.ExposedSettings.sentryDsn
  3. ? sentryReporter()
  4. : nullReporter()
  5. function sentryReporter() {
  6. return (
  7. import(/* webpackMode: "eager" */ '@sentry/browser')
  8. .then(Sentry => {
  9. let eventCount = 0
  10. Sentry.init({
  11. dsn: window.ExposedSettings.sentryDsn,
  12. environment: window.ExposedSettings.sentryEnvironment,
  13. release: window.ExposedSettings.sentryRelease,
  14. autoSessionTracking: false,
  15. // Ignore errors unless they come from our origins
  16. // Adapted from: https://docs.sentry.io/platforms/javascript/#decluttering-sentry
  17. whitelistUrls: [
  18. new RegExp(window.ExposedSettings.sentryAllowedOriginRegex),
  19. ],
  20. ignoreErrors: [
  21. // Ignore very noisy error
  22. 'SecurityError: Permission denied to access property "pathname" on cross-origin object',
  23. // Ignore unhandled error that is "expected" - see https://github.com/overleaf/issues/issues/3321
  24. /^Missing PDF/,
  25. /^pdfng error Error: MissingPDFException/,
  26. // Ignore "expected" error from aborted fetch - see https://github.com/overleaf/issues/issues/3321
  27. /^AbortError/,
  28. // Ignore spurious error from Ace internals - see https://github.com/overleaf/issues/issues/3321
  29. 'ResizeObserver loop limit exceeded',
  30. 'ResizeObserver loop completed with undelivered notifications.',
  31. // Microsoft Outlook SafeLink crawler
  32. // https://forum.sentry.io/t/unhandledrejection-non-error-promise-rejection-captured-with-value/14062
  33. /Non-Error promise rejection captured with value: Object Not Found Matching Id/,
  34. ],
  35. beforeSend(event) {
  36. // Limit number of events sent to Sentry to 100 events "per page load",
  37. // (i.e. the cap will be reset if the page is reloaded). This prevent
  38. // hitting their server-side event cap.
  39. eventCount++
  40. if (eventCount > 100) {
  41. return null // Block the event from sending
  42. } else {
  43. return event
  44. }
  45. },
  46. })
  47. Sentry.setUser({ id: window.user_id })
  48. return Sentry
  49. })
  50. // If Sentry fails to load, use the null reporter instead
  51. .catch(error => {
  52. console.error(error)
  53. return nullReporter()
  54. })
  55. )
  56. }
  57. function nullReporter() {
  58. return Promise.resolve({
  59. captureException: console.error,
  60. captureMessage: console.error,
  61. })
  62. }
  63. export function captureException(...args) {
  64. reporterPromise.then(reporter => reporter.captureException(...args))
  65. }
  66. export function captureMessage(...args) {
  67. reporterPromise.then(reporter => reporter.captureMessage(...args))
  68. }