1
1
import { captureException , getReportDialogEndpoint , withScope } from '@sentry/core' ;
2
2
import { 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' ;
4
12
5
13
const global = getGlobalObject < Window > ( ) ;
6
14
let ignoreOnError : number = 0 ;
@@ -39,19 +47,28 @@ export function wrap(
39
47
before ?: WrappedFunction ,
40
48
// eslint-disable-next-line @typescript-eslint/no-explicit-any
41
49
) : 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
+
42
57
if ( typeof fn !== 'function' ) {
43
58
return fn ;
44
59
}
45
60
46
61
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 ;
50
67
}
51
68
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 ;
55
72
}
56
73
} catch ( e ) {
57
74
// Just accessing custom props in some Selenium environments
@@ -73,14 +90,6 @@ export function wrap(
73
90
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
74
91
const wrappedArguments = args . map ( ( arg : any ) => wrap ( arg , options ) ) ;
75
92
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
- }
84
93
// Attempt to invoke user-land function
85
94
// NOTE: If you are a Sentry user, and you are seeing this stack frame, it
86
95
// means the sentry.javascript SDK caught an error invoking your application code. This
@@ -91,19 +100,17 @@ export function wrap(
91
100
92
101
withScope ( ( scope : Scope ) => {
93
102
scope . addEventProcessor ( ( event : SentryEvent ) => {
94
- const processedEvent = { ...event } ;
95
-
96
103
if ( options . mechanism ) {
97
- addExceptionTypeValue ( processedEvent , undefined , undefined ) ;
98
- addExceptionMechanism ( processedEvent , options . mechanism ) ;
104
+ addExceptionTypeValue ( event , undefined , undefined ) ;
105
+ addExceptionMechanism ( event , options . mechanism ) ;
99
106
}
100
107
101
- processedEvent . extra = {
102
- ...processedEvent . extra ,
108
+ event . extra = {
109
+ ...event . extra ,
103
110
arguments : args ,
104
111
} ;
105
112
106
- return processedEvent ;
113
+ return event ;
107
114
} ) ;
108
115
109
116
captureException ( ex ) ;
@@ -124,26 +131,11 @@ export function wrap(
124
131
}
125
132
} catch ( _oO ) { } // eslint-disable-line no-empty
126
133
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
-
135
134
// Signal that this function has been wrapped/filled already
136
135
// 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 ) ;
147
139
148
140
// Restore original function name (not all browsers allow that)
149
141
try {
0 commit comments