1
1
import type { AddRequestDataToEventOptions } from '@sentry/node' ;
2
2
import { captureException , flush , getCurrentHub } from '@sentry/node' ;
3
3
import { 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' ;
7
13
import type { HttpFunction , WrapperOptions } from './general' ;
8
14
9
15
// 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
105
111
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
106
112
( res as any ) . __sentry_transaction = transaction ;
107
113
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
-
115
114
// eslint-disable-next-line @typescript-eslint/unbound-method
116
115
const _end = res . end ;
117
116
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -128,6 +127,21 @@ function _wrapHttpFunction(fn: HttpFunction, wrapOptions: Partial<HttpFunctionWr
128
127
} ) ;
129
128
} ;
130
129
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 ;
132
146
} ;
133
147
}
0 commit comments