11/* eslint-disable @typescript-eslint/unbound-method */
22import {
3+ type Scope ,
34 captureException ,
45 flush ,
56 getClient ,
7+ isThenable ,
68 SEMANTIC_ATTRIBUTE_SENTRY_OP ,
79 SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN ,
810 startSpan ,
@@ -46,7 +48,8 @@ function wrapMethodWithSentry<T extends OriginalMethod>(
4648 const currentClient = getClient ( ) ;
4749 // if a client is already set, use withScope, otherwise use withIsolationScope
4850 const sentryWithScope = currentClient ? withScope : withIsolationScope ;
49- return sentryWithScope ( async scope => {
51+
52+ const wrappedFunction = ( scope : Scope ) : unknown => {
5053 // In certain situations, the passed context can become undefined.
5154 // For example, for Astro while prerendering pages at build time.
5255 // see: https://github.com/getsentry/sentry-javascript/issues/13217
@@ -65,17 +68,38 @@ function wrapMethodWithSentry<T extends OriginalMethod>(
6568 if ( callback ) {
6669 callback ( ...args ) ;
6770 }
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+ }
6994 } catch ( e ) {
7095 captureException ( e , {
7196 mechanism : {
7297 type : 'cloudflare_durableobject' ,
7398 handled : false ,
7499 } ,
75100 } ) ;
76- throw e ;
77- } finally {
78101 waitUntil ?.( flush ( 2000 ) ) ;
102+ throw e ;
79103 }
80104 }
81105
@@ -87,22 +111,45 @@ function wrapMethodWithSentry<T extends OriginalMethod>(
87111 : { } ;
88112
89113 // 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 } , ( ) => {
91115 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+ }
93139 } catch ( e ) {
94140 captureException ( e , {
95141 mechanism : {
96142 type : 'cloudflare_durableobject' ,
97143 handled : false ,
98144 } ,
99145 } ) ;
100- throw e ;
101- } finally {
102146 waitUntil ?.( flush ( 2000 ) ) ;
147+ throw e ;
103148 }
104149 } ) ;
105- } ) ;
150+ } ;
151+
152+ return sentryWithScope ( wrappedFunction ) ;
106153 } ,
107154 } ) ;
108155}
0 commit comments