@@ -319,35 +319,35 @@ export function wrapHandler<TEvent, TResult>(
319
319
} ) ;
320
320
}
321
321
322
- const scope = hub . pushScope ( ) ;
323
- let rv : TResult ;
324
- try {
325
- enhanceScopeWithEnvironmentData ( scope , context , START_TIME ) ;
326
- if ( options . startTrace ) {
327
- enhanceScopeWithTransactionData ( scope , context ) ;
328
- // We put the transaction on the scope so users can attach children to it
329
- scope . setSpan ( transaction ) ;
330
- }
331
- rv = await asyncHandler ( event , context ) ;
332
-
333
- // We manage lambdas that use Promise.allSettled by capturing the errors of failed promises
334
- if ( options . captureAllSettledReasons && Array . isArray ( rv ) && isPromiseAllSettledResult ( rv ) ) {
335
- const reasons = getRejectedReasons ( rv ) ;
336
- reasons . forEach ( exception => {
337
- captureException ( exception , scope => markEventUnhandled ( scope ) ) ;
322
+ return withScope ( async scope => {
323
+ let rv : TResult ;
324
+ try {
325
+ enhanceScopeWithEnvironmentData ( scope , context , START_TIME ) ;
326
+ if ( options . startTrace ) {
327
+ enhanceScopeWithTransactionData ( scope , context ) ;
328
+ // We put the transaction on the scope so users can attach children to it
329
+ scope . setSpan ( transaction ) ;
330
+ }
331
+ rv = await asyncHandler ( event , context ) ;
332
+
333
+ // We manage lambdas that use Promise.allSettled by capturing the errors of failed promises
334
+ if ( options . captureAllSettledReasons && Array . isArray ( rv ) && isPromiseAllSettledResult ( rv ) ) {
335
+ const reasons = getRejectedReasons ( rv ) ;
336
+ reasons . forEach ( exception => {
337
+ captureException ( exception , scope => markEventUnhandled ( scope ) ) ;
338
+ } ) ;
339
+ }
340
+ } catch ( e ) {
341
+ captureException ( e , scope => markEventUnhandled ( scope ) ) ;
342
+ throw e ;
343
+ } finally {
344
+ clearTimeout ( timeoutWarningTimer ) ;
345
+ transaction ?. finish ( ) ;
346
+ await flush ( options . flushTimeout ) . catch ( e => {
347
+ DEBUG_BUILD && logger . error ( e ) ;
338
348
} ) ;
339
349
}
340
- } catch ( e ) {
341
- captureException ( e , scope => markEventUnhandled ( scope ) ) ;
342
- throw e ;
343
- } finally {
344
- clearTimeout ( timeoutWarningTimer ) ;
345
- transaction ?. finish ( ) ;
346
- hub . popScope ( ) ;
347
- await flush ( options . flushTimeout ) . catch ( e => {
348
- DEBUG_BUILD && logger . error ( e ) ;
349
- } ) ;
350
- }
351
- return rv ;
350
+ return rv ;
351
+ } ) ;
352
352
} ;
353
353
}
0 commit comments