@@ -20,6 +20,9 @@ export const SentryCron = (monitorSlug: string, monitorConfig?: MonitorConfig):
20
20
monitorConfig ,
21
21
) ;
22
22
} ;
23
+
24
+ copyFunctionNameAndMetadata ( { originalMethod, descriptor } ) ;
25
+
23
26
return descriptor ;
24
27
} ;
25
28
} ;
@@ -28,7 +31,7 @@ export const SentryCron = (monitorSlug: string, monitorConfig?: MonitorConfig):
28
31
* A decorator usable to wrap arbitrary functions with spans.
29
32
*/
30
33
export function SentryTraced ( op : string = 'function' ) {
31
- return function ( target : unknown , propertyKey : string , descriptor : PropertyDescriptor ) {
34
+ return function ( _target : unknown , propertyKey : string , descriptor : PropertyDescriptor ) {
32
35
const originalMethod = descriptor . value as ( ...args : unknown [ ] ) => Promise < unknown > | unknown ; // function can be sync or async
33
36
34
37
descriptor . value = function ( ...args : unknown [ ] ) {
@@ -43,13 +46,7 @@ export function SentryTraced(op: string = 'function') {
43
46
) ;
44
47
} ;
45
48
46
- // preserve the original name on the decorated function
47
- Object . defineProperty ( descriptor . value , 'name' , {
48
- value : originalMethod . name ,
49
- configurable : true ,
50
- enumerable : true ,
51
- writable : true ,
52
- } ) ;
49
+ copyFunctionNameAndMetadata ( { originalMethod, descriptor } ) ;
53
50
54
51
return descriptor ;
55
52
} ;
@@ -71,6 +68,40 @@ export function SentryExceptionCaptured() {
71
68
return originalCatch . apply ( this , [ exception , host , ...args ] ) ;
72
69
} ;
73
70
71
+ copyFunctionNameAndMetadata ( { originalMethod : originalCatch , descriptor } ) ;
72
+
74
73
return descriptor ;
75
74
} ;
76
75
}
76
+
77
+ /**
78
+ * Copies the function name and metadata from the original method to the decorated method.
79
+ * This ensures that the decorated method maintains the same name and metadata as the original.
80
+ *
81
+ * @param {Function } params.originalMethod - The original method being decorated
82
+ * @param {PropertyDescriptor } params.descriptor - The property descriptor containing the decorated method
83
+ */
84
+ function copyFunctionNameAndMetadata ( {
85
+ originalMethod,
86
+ descriptor,
87
+ } : {
88
+ descriptor : PropertyDescriptor ;
89
+ originalMethod : ( ...args : unknown [ ] ) => unknown ;
90
+ } ) : void {
91
+ // preserve the original name on the decorated function
92
+ Object . defineProperty ( descriptor . value , 'name' , {
93
+ value : originalMethod . name ,
94
+ configurable : true ,
95
+ enumerable : true ,
96
+ writable : true ,
97
+ } ) ;
98
+
99
+ // copy metadata
100
+ if ( typeof Reflect !== 'undefined' && typeof Reflect . getMetadataKeys === 'function' ) {
101
+ const originalMetaData = Reflect . getMetadataKeys ( originalMethod ) ;
102
+ for ( const key of originalMetaData ) {
103
+ const value = Reflect . getMetadata ( key , originalMethod ) ;
104
+ Reflect . defineMetadata ( key , value , descriptor . value ) ;
105
+ }
106
+ }
107
+ }
0 commit comments