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