@@ -32,50 +32,50 @@ export const withSentry = (handler: NextApiHandler): WrappedNextApiHandler => {
32
32
// return a value. In our case, all any of the codepaths return is a promise of `void`, but nextjs still counts on
33
33
// getting that before it will finish the response.
34
34
const boundHandler = local . bind ( async ( ) => {
35
- try {
36
- const currentScope = getCurrentHub ( ) . getScope ( ) ;
35
+ const currentScope = getCurrentHub ( ) . getScope ( ) ;
37
36
38
- if ( currentScope ) {
39
- currentScope . addEventProcessor ( event => addRequestDataToEvent ( event , req as NextRequest ) ) ;
37
+ if ( currentScope ) {
38
+ currentScope . addEventProcessor ( event => addRequestDataToEvent ( event , req as NextRequest ) ) ;
40
39
41
- if ( hasTracingEnabled ( ) ) {
42
- // If there is a trace header set, extract the data from it (parentSpanId, traceId, and sampling decision)
43
- let traceparentData ;
44
- if ( req . headers && isString ( req . headers [ 'sentry-trace' ] ) ) {
45
- traceparentData = extractTraceparentData ( req . headers [ 'sentry-trace' ] as string ) ;
46
- logger . log ( `[Tracing] Continuing trace ${ traceparentData ?. traceId } .` ) ;
47
- }
40
+ if ( hasTracingEnabled ( ) ) {
41
+ // If there is a trace header set, extract the data from it (parentSpanId, traceId, and sampling decision)
42
+ let traceparentData ;
43
+ if ( req . headers && isString ( req . headers [ 'sentry-trace' ] ) ) {
44
+ traceparentData = extractTraceparentData ( req . headers [ 'sentry-trace' ] as string ) ;
45
+ logger . log ( `[Tracing] Continuing trace ${ traceparentData ?. traceId } .` ) ;
46
+ }
48
47
49
- const url = `${ req . url } ` ;
50
- // pull off query string, if any
51
- let reqPath = stripUrlQueryAndFragment ( url ) ;
52
- // Replace with placeholder
53
- if ( req . query ) {
54
- // TODO get this from next if possible, to avoid accidentally replacing non-dynamic parts of the path if
55
- // they match dynamic parts
56
- for ( const [ key , value ] of Object . entries ( req . query ) ) {
57
- reqPath = reqPath . replace ( `${ value } ` , `[${ key } ]` ) ;
58
- }
48
+ const url = `${ req . url } ` ;
49
+ // pull off query string, if any
50
+ let reqPath = stripUrlQueryAndFragment ( url ) ;
51
+ // Replace with placeholder
52
+ if ( req . query ) {
53
+ // TODO get this from next if possible, to avoid accidentally replacing non-dynamic parts of the path if
54
+ // they match dynamic parts
55
+ for ( const [ key , value ] of Object . entries ( req . query ) ) {
56
+ reqPath = reqPath . replace ( `${ value } ` , `[${ key } ]` ) ;
59
57
}
60
- const reqMethod = `${ ( req . method || 'GET' ) . toUpperCase ( ) } ` ;
61
-
62
- const transaction = startTransaction (
63
- {
64
- name : `${ reqMethod } ${ reqPath } ` ,
65
- op : 'http.server' ,
66
- ...traceparentData ,
67
- } ,
68
- // extra context passed to the `tracesSampler`
69
- { request : req } ,
70
- ) ;
71
- currentScope . setSpan ( transaction ) ;
72
-
73
- // save a link to the scope on the response, so that even if there's an error (landing us outside of
74
- // the domain), we can still finish the transaction and attach the correct data to it
75
- ( res as ScopedResponse ) . __sentryScope = currentScope ;
76
58
}
59
+ const reqMethod = `${ ( req . method || 'GET' ) . toUpperCase ( ) } ` ;
60
+
61
+ const transaction = startTransaction (
62
+ {
63
+ name : `${ reqMethod } ${ reqPath } ` ,
64
+ op : 'http.server' ,
65
+ ...traceparentData ,
66
+ } ,
67
+ // extra context passed to the `tracesSampler`
68
+ { request : req } ,
69
+ ) ;
70
+ currentScope . setSpan ( transaction ) ;
71
+
72
+ // save a link to the scope on the response, so that even if there's an error (landing us outside of
73
+ // the domain), we can still finish the transaction and attach the correct data to it
74
+ ( res as ScopedResponse ) . __sentryScope = currentScope ;
77
75
}
76
+ }
78
77
78
+ try {
79
79
return await handler ( req , res ) ; // Call original handler
80
80
} catch ( e ) {
81
81
withScope ( scope => {
0 commit comments