1
1
/* eslint-disable @typescript-eslint/unbound-method */
2
2
import {
3
+ type Scope ,
3
4
captureException ,
4
5
flush ,
5
6
getClient ,
7
+ isThenable ,
6
8
SEMANTIC_ATTRIBUTE_SENTRY_OP ,
7
9
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN ,
8
10
startSpan ,
@@ -46,7 +48,8 @@ function wrapMethodWithSentry<T extends OriginalMethod>(
46
48
const currentClient = getClient ( ) ;
47
49
// if a client is already set, use withScope, otherwise use withIsolationScope
48
50
const sentryWithScope = currentClient ? withScope : withIsolationScope ;
49
- return sentryWithScope ( async scope => {
51
+
52
+ const wrappedFunction = ( scope : Scope ) : unknown => {
50
53
// In certain situations, the passed context can become undefined.
51
54
// For example, for Astro while prerendering pages at build time.
52
55
// see: https://github.com/getsentry/sentry-javascript/issues/13217
@@ -65,7 +68,28 @@ function wrapMethodWithSentry<T extends OriginalMethod>(
65
68
if ( callback ) {
66
69
callback ( ...args ) ;
67
70
}
68
- return await Reflect . apply ( target , thisArg , args ) ;
71
+ const result = Reflect . apply ( target , thisArg , args ) ;
72
+
73
+ if ( isThenable ( result ) ) {
74
+ return result . then (
75
+ ( res : unknown ) => {
76
+ waitUntil ?.( flush ( 2000 ) ) ;
77
+ return res ;
78
+ } ,
79
+ ( e : unknown ) => {
80
+ captureException ( e , {
81
+ mechanism : {
82
+ type : 'cloudflare_durableobject' ,
83
+ handled : false ,
84
+ } ,
85
+ } ) ;
86
+ waitUntil ?.( flush ( 2000 ) ) ;
87
+ throw e ;
88
+ } ,
89
+ ) ;
90
+ } else {
91
+ return result ;
92
+ }
69
93
} catch ( e ) {
70
94
captureException ( e , {
71
95
mechanism : {
@@ -87,9 +111,30 @@ function wrapMethodWithSentry<T extends OriginalMethod>(
87
111
: { } ;
88
112
89
113
// Only create these spans if they have a parent span.
90
- return startSpan ( { name : wrapperOptions . spanName , attributes, onlyIfParent : true } , async ( ) => {
114
+ return startSpan ( { name : wrapperOptions . spanName , attributes, onlyIfParent : true } , ( ) => {
91
115
try {
92
- return await Reflect . apply ( target , thisArg , args ) ;
116
+ const result = Reflect . apply ( target , thisArg , args ) ;
117
+
118
+ if ( isThenable ( result ) ) {
119
+ return result . then (
120
+ ( res : unknown ) => {
121
+ waitUntil ?.( flush ( 2000 ) ) ;
122
+ return res ;
123
+ } ,
124
+ ( e : unknown ) => {
125
+ captureException ( e , {
126
+ mechanism : {
127
+ type : 'cloudflare_durableobject' ,
128
+ handled : false ,
129
+ } ,
130
+ } ) ;
131
+ waitUntil ?.( flush ( 2000 ) ) ;
132
+ throw e ;
133
+ } ,
134
+ ) ;
135
+ } else {
136
+ return result ;
137
+ }
93
138
} catch ( e ) {
94
139
captureException ( e , {
95
140
mechanism : {
@@ -102,7 +147,9 @@ function wrapMethodWithSentry<T extends OriginalMethod>(
102
147
waitUntil ?.( flush ( 2000 ) ) ;
103
148
}
104
149
} ) ;
105
- } ) ;
150
+ } ;
151
+
152
+ return sentryWithScope ( wrappedFunction ) ;
106
153
} ,
107
154
} ) ;
108
155
}
0 commit comments