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,17 +68,38 @@ 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
+ waitUntil ?.( flush ( 2000 ) ) ;
92
+ return result ;
93
+ }
69
94
} catch ( e ) {
70
95
captureException ( e , {
71
96
mechanism : {
72
97
type : 'cloudflare_durableobject' ,
73
98
handled : false ,
74
99
} ,
75
100
} ) ;
76
- throw e ;
77
- } finally {
78
101
waitUntil ?.( flush ( 2000 ) ) ;
102
+ throw e ;
79
103
}
80
104
}
81
105
@@ -87,22 +111,45 @@ 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
+ waitUntil ?.( flush ( 2000 ) ) ;
137
+ return result ;
138
+ }
93
139
} catch ( e ) {
94
140
captureException ( e , {
95
141
mechanism : {
96
142
type : 'cloudflare_durableobject' ,
97
143
handled : false ,
98
144
} ,
99
145
} ) ;
100
- throw e ;
101
- } finally {
102
146
waitUntil ?.( flush ( 2000 ) ) ;
147
+ throw e ;
103
148
}
104
149
} ) ;
105
- } ) ;
150
+ } ;
151
+
152
+ return sentryWithScope ( wrappedFunction ) ;
106
153
} ,
107
154
} ) ;
108
155
}
0 commit comments