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,7 +68,28 @@ 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+ return result ;
92+ }
6993 } catch ( e ) {
7094 captureException ( e , {
7195 mechanism : {
@@ -87,9 +111,30 @@ 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+ return result ;
137+ }
93138 } catch ( e ) {
94139 captureException ( e , {
95140 mechanism : {
@@ -102,7 +147,9 @@ function wrapMethodWithSentry<T extends OriginalMethod>(
102147 waitUntil ?.( flush ( 2000 ) ) ;
103148 }
104149 } ) ;
105- } ) ;
150+ } ;
151+
152+ return sentryWithScope ( wrappedFunction ) ;
106153 } ,
107154 } ) ;
108155}
0 commit comments