globalError.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { GLOBAL_OBJ } from '../worldwide.js';
  2. import { addHandler, maybeInstrument, triggerHandlers } from './_handlers.js';
  3. let _oldOnErrorHandler = null;
  4. /**
  5. * Add an instrumentation handler for when an error is captured by the global error handler.
  6. *
  7. * Use at your own risk, this might break without changelog notice, only used internally.
  8. * @hidden
  9. */
  10. function addGlobalErrorInstrumentationHandler(handler) {
  11. const type = 'error';
  12. addHandler(type, handler);
  13. maybeInstrument(type, instrumentError);
  14. }
  15. function instrumentError() {
  16. _oldOnErrorHandler = GLOBAL_OBJ.onerror;
  17. GLOBAL_OBJ.onerror = function (
  18. msg,
  19. url,
  20. line,
  21. column,
  22. error,
  23. ) {
  24. const handlerData = {
  25. column,
  26. error,
  27. line,
  28. msg,
  29. url,
  30. };
  31. triggerHandlers('error', handlerData);
  32. if (_oldOnErrorHandler && !_oldOnErrorHandler.__SENTRY_LOADER__) {
  33. // eslint-disable-next-line prefer-rest-params
  34. return _oldOnErrorHandler.apply(this, arguments);
  35. }
  36. return false;
  37. };
  38. GLOBAL_OBJ.onerror.__SENTRY_INSTRUMENTED__ = true;
  39. }
  40. export { addGlobalErrorInstrumentationHandler };
  41. //# sourceMappingURL=globalError.js.map