11import { captureException , getReportDialogEndpoint , withScope } from '@sentry/core' ;
22import { DsnLike , Event as SentryEvent , Mechanism , Scope , WrappedFunction } from '@sentry/types' ;
3- import { addExceptionMechanism , addExceptionTypeValue , getGlobalObject , logger } from '@sentry/utils' ;
3+ import {
4+ addExceptionMechanism ,
5+ addExceptionTypeValue ,
6+ addNonEnumerableProperty ,
7+ getGlobalObject ,
8+ getOriginalFunction ,
9+ logger ,
10+ markFunctionWrapped ,
11+ } from '@sentry/utils' ;
412
513const global = getGlobalObject < Window > ( ) ;
614let ignoreOnError : number = 0 ;
@@ -39,19 +47,28 @@ export function wrap(
3947 before ?: WrappedFunction ,
4048 // eslint-disable-next-line @typescript-eslint/no-explicit-any
4149) : any {
50+ // for future readers what this does is wrap a function and then create
51+ // a bi-directional wrapping between them.
52+ //
53+ // example: wrapped = wrap(original);
54+ // original.__sentry_wrapped__ -> wrapped
55+ // wrapped.__sentry_original__ -> original
56+
4257 if ( typeof fn !== 'function' ) {
4358 return fn ;
4459 }
4560
4661 try {
47- // We don't wanna wrap it twice
48- if ( fn . __sentry__ ) {
49- return fn ;
62+ // if we're dealing with a function that was previously wrapped, return
63+ // the original wrapper.
64+ const wrapper = fn . __sentry_wrapped__ ;
65+ if ( wrapper ) {
66+ return wrapper ;
5067 }
5168
52- // If this has already been wrapped in the past, return that wrapped function
53- if ( fn . __sentry_wrapped__ ) {
54- return fn . __sentry_wrapped__ ;
69+ // We don't wanna wrap it twice
70+ if ( getOriginalFunction ( fn ) ) {
71+ return fn ;
5572 }
5673 } catch ( e ) {
5774 // Just accessing custom props in some Selenium environments
@@ -73,14 +90,6 @@ export function wrap(
7390 // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
7491 const wrappedArguments = args . map ( ( arg : any ) => wrap ( arg , options ) ) ;
7592
76- if ( fn . handleEvent ) {
77- // Attempt to invoke user-land function
78- // NOTE: If you are a Sentry user, and you are seeing this stack frame, it
79- // means the sentry.javascript SDK caught an error invoking your application code. This
80- // is expected behavior and NOT indicative of a bug with sentry.javascript.
81- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
82- return fn . handleEvent . apply ( this , wrappedArguments ) ;
83- }
8493 // Attempt to invoke user-land function
8594 // NOTE: If you are a Sentry user, and you are seeing this stack frame, it
8695 // means the sentry.javascript SDK caught an error invoking your application code. This
@@ -91,19 +100,17 @@ export function wrap(
91100
92101 withScope ( ( scope : Scope ) => {
93102 scope . addEventProcessor ( ( event : SentryEvent ) => {
94- const processedEvent = { ...event } ;
95-
96103 if ( options . mechanism ) {
97- addExceptionTypeValue ( processedEvent , undefined , undefined ) ;
98- addExceptionMechanism ( processedEvent , options . mechanism ) ;
104+ addExceptionTypeValue ( event , undefined , undefined ) ;
105+ addExceptionMechanism ( event , options . mechanism ) ;
99106 }
100107
101- processedEvent . extra = {
102- ...processedEvent . extra ,
108+ event . extra = {
109+ ...event . extra ,
103110 arguments : args ,
104111 } ;
105112
106- return processedEvent ;
113+ return event ;
107114 } ) ;
108115
109116 captureException ( ex ) ;
@@ -124,26 +131,11 @@ export function wrap(
124131 }
125132 } catch ( _oO ) { } // eslint-disable-line no-empty
126133
127- fn . prototype = fn . prototype || { } ;
128- sentryWrapped . prototype = fn . prototype ;
129-
130- Object . defineProperty ( fn , '__sentry_wrapped__' , {
131- enumerable : false ,
132- value : sentryWrapped ,
133- } ) ;
134-
135134 // Signal that this function has been wrapped/filled already
136135 // for both debugging and to prevent it to being wrapped/filled twice
137- Object . defineProperties ( sentryWrapped , {
138- __sentry__ : {
139- enumerable : false ,
140- value : true ,
141- } ,
142- __sentry_original__ : {
143- enumerable : false ,
144- value : fn ,
145- } ,
146- } ) ;
136+ markFunctionWrapped ( sentryWrapped , fn ) ;
137+
138+ addNonEnumerableProperty ( fn , '__sentry_wrapped__' , sentryWrapped ) ;
147139
148140 // Restore original function name (not all browsers allow that)
149141 try {
0 commit comments