wrapPageComponentWithSentry.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. var {
  2. _optionalChain
  3. } = require('@sentry/utils');
  4. Object.defineProperty(exports, '__esModule', { value: true });
  5. const core = require('@sentry/core');
  6. const utils = require('@sentry/utils');
  7. function isReactClassComponent(target) {
  8. // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
  9. return typeof target === 'function' && _optionalChain([target, 'optionalAccess', _ => _.prototype, 'optionalAccess', _2 => _2.isReactComponent]);
  10. }
  11. /**
  12. * Wraps a page component with Sentry error instrumentation.
  13. */
  14. function wrapPageComponentWithSentry(pageComponent) {
  15. core.addTracingExtensions();
  16. if (isReactClassComponent(pageComponent)) {
  17. return class SentryWrappedPageComponent extends pageComponent {
  18. render(...args) {
  19. return core.runWithAsyncContext(() => {
  20. const scope = core.getCurrentScope();
  21. // We extract the sentry trace data that is put in the component props by datafetcher wrappers
  22. const sentryTraceData =
  23. typeof this.props === 'object' &&
  24. this.props !== null &&
  25. '_sentryTraceData' in this.props &&
  26. typeof this.props._sentryTraceData === 'string'
  27. ? this.props._sentryTraceData
  28. : undefined;
  29. if (sentryTraceData) {
  30. const traceparentData = utils.extractTraceparentData(sentryTraceData);
  31. scope.setContext('trace', {
  32. span_id: _optionalChain([traceparentData, 'optionalAccess', _3 => _3.parentSpanId]),
  33. trace_id: _optionalChain([traceparentData, 'optionalAccess', _4 => _4.traceId]),
  34. });
  35. }
  36. try {
  37. return super.render(...args);
  38. } catch (e) {
  39. core.captureException(e, {
  40. mechanism: {
  41. handled: false,
  42. },
  43. });
  44. throw e;
  45. }
  46. });
  47. }
  48. };
  49. } else if (typeof pageComponent === 'function') {
  50. return new Proxy(pageComponent, {
  51. apply(target, thisArg, argArray) {
  52. return core.runWithAsyncContext(() => {
  53. const scope = core.getCurrentScope();
  54. // We extract the sentry trace data that is put in the component props by datafetcher wrappers
  55. const sentryTraceData = _optionalChain([argArray, 'optionalAccess', _5 => _5[0], 'optionalAccess', _6 => _6._sentryTraceData]);
  56. if (sentryTraceData) {
  57. const traceparentData = utils.extractTraceparentData(sentryTraceData);
  58. scope.setContext('trace', {
  59. span_id: _optionalChain([traceparentData, 'optionalAccess', _7 => _7.parentSpanId]),
  60. trace_id: _optionalChain([traceparentData, 'optionalAccess', _8 => _8.traceId]),
  61. });
  62. }
  63. try {
  64. return target.apply(thisArg, argArray);
  65. } catch (e) {
  66. core.captureException(e, {
  67. mechanism: {
  68. handled: false,
  69. },
  70. });
  71. throw e;
  72. }
  73. });
  74. },
  75. });
  76. } else {
  77. return pageComponent;
  78. }
  79. }
  80. exports.wrapPageComponentWithSentry = wrapPageComponentWithSentry;
  81. //# sourceMappingURL=wrapPageComponentWithSentry.js.map