error-reporter.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. // Ignore "expected" error from aborted fetch - see https://github.com/overleaf/issues/issues/3321
  26. /^AbortError/,
  27. // Ignore spurious error from Ace internals - see https://github.com/overleaf/issues/issues/3321
  28. 'ResizeObserver loop limit exceeded',
  29. 'ResizeObserver loop completed with undelivered notifications.',
  30. // Microsoft Outlook SafeLink crawler
  31. // https://forum.sentry.io/t/unhandledrejection-non-error-promise-rejection-captured-with-value/14062
  32. /Non-Error promise rejection captured with value: Object Not Found Matching Id/,
  33. ],
  34. beforeSend(event) {
  35. // Limit number of events sent to Sentry to 100 events "per page load",
  36. // (i.e. the cap will be reset if the page is reloaded). This prevent
  37. // hitting their server-side event cap.
  38. eventCount++
  39. if (eventCount > 100) {
  40. return null // Block the event from sending
  41. } else {
  42. return event
  43. }
  44. },
  45. })
  46. Sentry.setUser({ id: window.user_id })
  47. return Sentry
  48. })
  49. // If Sentry fails to load, use the null reporter instead
  50. .catch(error => {
  51. console.error(error)
  52. return nullReporter()
  53. })
  54. )
  55. }
  56. function nullReporter() {
  57. return Promise.resolve({
  58. captureException: console.error,
  59. captureMessage: console.error,
  60. })
  61. }
  62. export function captureException(...args) {
  63. reporterPromise.then(reporter => reporter.captureException(...args))
  64. }
  65. export function captureMessage(...args) {
  66. reporterPromise.then(reporter => reporter.captureMessage(...args))
  67. }