error-reporter.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. release: window.ExposedSettings.sentryRelease,
  13. // Ignore errors unless they come from our origins
  14. // Adapted from: https://docs.sentry.io/platforms/javascript/#decluttering-sentry
  15. whitelistUrls: [
  16. new RegExp(window.ExposedSettings.sentryAllowedOriginRegex)
  17. ],
  18. ignoreErrors: [
  19. // Ignore very noisy error
  20. 'SecurityError: Permission denied to access property "pathname" on cross-origin object',
  21. // Ignore unhandled error that is "expected" - see https://github.com/overleaf/issues/issues/3321
  22. /^Missing PDF/,
  23. // Ignore "expected" error from aborted fetch - see https://github.com/overleaf/issues/issues/3321
  24. /^AbortError/,
  25. // Ignore spurious error from Ace internals - see https://github.com/overleaf/issues/issues/3321
  26. 'ResizeObserver loop limit exceeded',
  27. 'ResizeObserver loop completed with undelivered notifications.'
  28. ],
  29. beforeSend(event) {
  30. // Limit number of events sent to Sentry to 100 events "per page load",
  31. // (i.e. the cap will be reset if the page is reloaded). This prevent
  32. // hitting their server-side event cap.
  33. eventCount++
  34. if (eventCount > 100) {
  35. return null // Block the event from sending
  36. } else {
  37. return event
  38. }
  39. }
  40. })
  41. Sentry.setUser({ id: window.user_id })
  42. return Sentry
  43. })
  44. // If Sentry fails to load, use the null reporter instead
  45. .catch(nullReporter)
  46. )
  47. }
  48. function nullReporter() {
  49. return Promise.resolve({
  50. captureException: error => {
  51. console.error(error)
  52. },
  53. captureMessage: error => {
  54. console.error(error)
  55. }
  56. })
  57. }
  58. export function captureException(...args) {
  59. reporterPromise.then(reporter => reporter.captureException(...args))
  60. }
  61. export function captureMessage(...args) {
  62. reporterPromise.then(reporter => reporter.captureMessage(...args))
  63. }