@@ -3,10 +3,10 @@ import {
33 addTracingExtensions ,
44 captureException ,
55 getActiveSpan ,
6- getIsolationScope ,
76 getRootSpan ,
87 handleCallbackErrors ,
98 setHttpStatus ,
9+ withIsolationScope ,
1010} from '@sentry/core' ;
1111import { winterCGHeadersToDict } from '@sentry/utils' ;
1212import { isNotFoundNavigationError , isRedirectNavigationError } from './nextNavigationErrorUtils' ;
@@ -28,50 +28,52 @@ export function wrapRouteHandlerWithSentry<F extends (...args: any[]) => any>(
2828
2929 return new Proxy ( routeHandler , {
3030 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 ,
5635 } ,
57- ) ;
36+ } ) ;
5837
5938 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?
6266 }
63- } catch {
64- // best effort - response may be undefined?
65- }
6667
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+ }
7375 }
74- }
76+ } ) ;
7577 } ,
7678 } ) ;
7779}
0 commit comments