11import type { AddRequestDataToEventOptions } from '@sentry/node' ;
22import { captureException , flush , getCurrentHub } from '@sentry/node' ;
33import { extractTraceparentData } from '@sentry/tracing' ;
4- import { baggageHeaderToDynamicSamplingContext , isString , logger , stripUrlQueryAndFragment } from '@sentry/utils' ;
5-
6- import { domainify , getActiveDomain , proxyFunction } from './../utils' ;
4+ import {
5+ baggageHeaderToDynamicSamplingContext ,
6+ isString ,
7+ isThenable ,
8+ logger ,
9+ stripUrlQueryAndFragment ,
10+ } from '@sentry/utils' ;
11+
12+ import { domainify , proxyFunction } from './../utils' ;
713import type { HttpFunction , WrapperOptions } from './general' ;
814
915// TODO (v8 / #5257): Remove this whole old/new business and just use the new stuff
@@ -105,13 +111,6 @@ function _wrapHttpFunction(fn: HttpFunction, wrapOptions: Partial<HttpFunctionWr
105111 // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
106112 ( res as any ) . __sentry_transaction = transaction ;
107113
108- // functions-framework creates a domain for each incoming request so we take advantage of this fact and add an error handler.
109- // BTW this is the only way to catch any exception occured during request lifecycle.
110- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
111- getActiveDomain ( ) ! . on ( 'error' , err => {
112- captureException ( err ) ;
113- } ) ;
114-
115114 // eslint-disable-next-line @typescript-eslint/unbound-method
116115 const _end = res . end ;
117116 // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -128,6 +127,21 @@ function _wrapHttpFunction(fn: HttpFunction, wrapOptions: Partial<HttpFunctionWr
128127 } ) ;
129128 } ;
130129
131- return fn ( req , res ) ;
130+ let fnResult ;
131+ try {
132+ fnResult = fn ( req , res ) ;
133+ } catch ( err ) {
134+ captureException ( err ) ;
135+ throw err ;
136+ }
137+
138+ if ( isThenable ( fnResult ) ) {
139+ fnResult . then ( null , err => {
140+ captureException ( err ) ;
141+ throw err ;
142+ } ) ;
143+ }
144+
145+ return fnResult ;
132146 } ;
133147}
0 commit comments