@@ -3,10 +3,10 @@ import {
3
3
addTracingExtensions ,
4
4
captureException ,
5
5
getActiveSpan ,
6
- getIsolationScope ,
7
6
getRootSpan ,
8
7
handleCallbackErrors ,
9
8
setHttpStatus ,
9
+ withIsolationScope ,
10
10
} from '@sentry/core' ;
11
11
import { winterCGHeadersToDict } from '@sentry/utils' ;
12
12
import { isNotFoundNavigationError , isRedirectNavigationError } from './nextNavigationErrorUtils' ;
@@ -28,50 +28,52 @@ export function wrapRouteHandlerWithSentry<F extends (...args: any[]) => any>(
28
28
29
29
return new Proxy ( routeHandler , {
30
30
apply : async ( originalFunction , thisArg , args ) => {
31
- getIsolationScope ( ) . setSDKProcessingMetadata ( {
32
- request : {
33
- headers : headers ? winterCGHeadersToDict ( headers ) : undefined ,
34
- } ,
35
- } ) ;
36
-
37
- try {
38
- const activeSpan = getActiveSpan ( ) ;
39
- const rootSpan = activeSpan && getRootSpan ( activeSpan ) ;
40
-
41
- const response : Response = await handleCallbackErrors (
42
- ( ) => originalFunction . apply ( thisArg , args ) ,
43
- error => {
44
- // Next.js throws errors when calling `redirect()`. We don't wanna report these.
45
- if ( isRedirectNavigationError ( error ) ) {
46
- // Don't do anything
47
- } else if ( isNotFoundNavigationError ( error ) && rootSpan ) {
48
- rootSpan . setStatus ( { code : SPAN_STATUS_ERROR , message : 'not_found' } ) ;
49
- } else {
50
- captureException ( error , {
51
- mechanism : {
52
- handled : false ,
53
- } ,
54
- } ) ;
55
- }
31
+ return withIsolationScope ( async isolationScope => {
32
+ isolationScope . setSDKProcessingMetadata ( {
33
+ request : {
34
+ headers : headers ? winterCGHeadersToDict ( headers ) : undefined ,
56
35
} ,
57
- ) ;
36
+ } ) ;
58
37
59
38
try {
60
- if ( rootSpan && response . status ) {
61
- setHttpStatus ( rootSpan , response . status ) ;
39
+ const activeSpan = getActiveSpan ( ) ;
40
+ const rootSpan = activeSpan && getRootSpan ( activeSpan ) ;
41
+
42
+ const response : Response = await handleCallbackErrors (
43
+ ( ) => originalFunction . apply ( thisArg , args ) ,
44
+ error => {
45
+ // Next.js throws errors when calling `redirect()`. We don't wanna report these.
46
+ if ( isRedirectNavigationError ( error ) ) {
47
+ // Don't do anything
48
+ } else if ( isNotFoundNavigationError ( error ) && rootSpan ) {
49
+ rootSpan . setStatus ( { code : SPAN_STATUS_ERROR , message : 'not_found' } ) ;
50
+ } else {
51
+ captureException ( error , {
52
+ mechanism : {
53
+ handled : false ,
54
+ } ,
55
+ } ) ;
56
+ }
57
+ } ,
58
+ ) ;
59
+
60
+ try {
61
+ if ( rootSpan && response . status ) {
62
+ setHttpStatus ( rootSpan , response . status ) ;
63
+ }
64
+ } catch {
65
+ // best effort - response may be undefined?
62
66
}
63
- } catch {
64
- // best effort - response may be undefined?
65
- }
66
67
67
- return response ;
68
- } finally {
69
- if ( ! platformSupportsStreaming ( ) || process . env . NEXT_RUNTIME === 'edge' ) {
70
- // 1. Edge transport requires manual flushing
71
- // 2. Lambdas require manual flushing to prevent execution freeze before the event is sent
72
- await flushQueue ( ) ;
68
+ return response ;
69
+ } finally {
70
+ if ( ! platformSupportsStreaming ( ) || process . env . NEXT_RUNTIME === 'edge' ) {
71
+ // 1. Edge transport requires manual flushing
72
+ // 2. Lambdas require manual flushing to prevent execution freeze before the event is sent
73
+ await flushQueue ( ) ;
74
+ }
73
75
}
74
- }
76
+ } ) ;
75
77
} ,
76
78
} ) ;
77
79
}
0 commit comments