@@ -4,11 +4,11 @@ import {
4
4
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE ,
5
5
SPAN_STATUS_ERROR ,
6
6
captureException ,
7
- getCurrentScope ,
8
7
handleCallbackErrors ,
9
8
setHttpStatus ,
10
9
startSpan ,
11
10
withIsolationScope ,
11
+ withScope ,
12
12
} from '@sentry/core' ;
13
13
import { propagationContextFromHeaders , winterCGHeadersToDict } from '@sentry/utils' ;
14
14
import { isNotFoundNavigationError , isRedirectNavigationError } from './nextNavigationErrorUtils' ;
@@ -51,57 +51,59 @@ export function wrapRouteHandlerWithSentry<F extends (...args: any[]) => any>(
51
51
52
52
const propagationContext = commonObjectToPropagationContext ( headers , incomingPropagationContext ) ;
53
53
54
- return withIsolationScope ( isolationScope , async ( ) => {
55
- isolationScope . setTransactionName ( `${ method } ${ parameterizedRoute } ` ) ;
56
- getCurrentScope ( ) . setPropagationContext ( propagationContext ) ;
57
- try {
58
- return startSpan (
59
- {
60
- name : `${ method } ${ parameterizedRoute } ` ,
61
- attributes : {
62
- [ SEMANTIC_ATTRIBUTE_SENTRY_SOURCE ] : 'route' ,
63
- [ SEMANTIC_ATTRIBUTE_SENTRY_OP ] : 'http.server' ,
64
- [ SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN ] : 'auto.function.nextjs' ,
65
- } ,
66
- forceTransaction : true ,
67
- } ,
68
- async span => {
69
- const response : Response = await handleCallbackErrors (
70
- ( ) => originalFunction . apply ( thisArg , args ) ,
71
- error => {
72
- // Next.js throws errors when calling `redirect()`. We don't wanna report these.
73
- if ( isRedirectNavigationError ( error ) ) {
74
- // Don't do anything
75
- } else if ( isNotFoundNavigationError ( error ) && span ) {
76
- span . setStatus ( { code : SPAN_STATUS_ERROR , message : 'not_found' } ) ;
77
- } else {
78
- captureException ( error , {
79
- mechanism : {
80
- handled : false ,
81
- } ,
82
- } ) ;
83
- }
54
+ return withIsolationScope ( isolationScope , ( ) => {
55
+ return withScope ( async scope => {
56
+ scope . setTransactionName ( `${ method } ${ parameterizedRoute } ` ) ;
57
+ scope . setPropagationContext ( propagationContext ) ;
58
+ try {
59
+ return startSpan (
60
+ {
61
+ name : `${ method } ${ parameterizedRoute } ` ,
62
+ attributes : {
63
+ [ SEMANTIC_ATTRIBUTE_SENTRY_SOURCE ] : 'route' ,
64
+ [ SEMANTIC_ATTRIBUTE_SENTRY_OP ] : 'http.server' ,
65
+ [ SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN ] : 'auto.function.nextjs' ,
84
66
} ,
85
- ) ;
67
+ forceTransaction : true ,
68
+ } ,
69
+ async span => {
70
+ const response : Response = await handleCallbackErrors (
71
+ ( ) => originalFunction . apply ( thisArg , args ) ,
72
+ error => {
73
+ // Next.js throws errors when calling `redirect()`. We don't wanna report these.
74
+ if ( isRedirectNavigationError ( error ) ) {
75
+ // Don't do anything
76
+ } else if ( isNotFoundNavigationError ( error ) && span ) {
77
+ span . setStatus ( { code : SPAN_STATUS_ERROR , message : 'not_found' } ) ;
78
+ } else {
79
+ captureException ( error , {
80
+ mechanism : {
81
+ handled : false ,
82
+ } ,
83
+ } ) ;
84
+ }
85
+ } ,
86
+ ) ;
86
87
87
- try {
88
- if ( span && response . status ) {
89
- setHttpStatus ( span , response . status ) ;
88
+ try {
89
+ if ( span && response . status ) {
90
+ setHttpStatus ( span , response . status ) ;
91
+ }
92
+ } catch {
93
+ // best effort - response may be undefined?
90
94
}
91
- } catch {
92
- // best effort - response may be undefined?
93
- }
94
95
95
- return response ;
96
- } ,
97
- ) ;
98
- } finally {
99
- if ( ! platformSupportsStreaming ( ) || process . env . NEXT_RUNTIME === 'edge' ) {
100
- // 1. Edge transport requires manual flushing
101
- // 2. Lambdas require manual flushing to prevent execution freeze before the event is sent
102
- await flushQueue ( ) ;
96
+ return response ;
97
+ } ,
98
+ ) ;
99
+ } finally {
100
+ if ( ! platformSupportsStreaming ( ) || process . env . NEXT_RUNTIME === 'edge' ) {
101
+ // 1. Edge transport requires manual flushing
102
+ // 2. Lambdas require manual flushing to prevent execution freeze before the event is sent
103
+ await flushQueue ( ) ;
104
+ }
103
105
}
104
- }
106
+ } ) ;
105
107
} ) ;
106
108
} ) ;
107
109
} ,
0 commit comments