@@ -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' ;
@@ -29,50 +29,52 @@ export function wrapRouteHandlerWithSentry<F extends (...args: any[]) => any>(
2929
3030 return new Proxy ( routeHandler , {
3131 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 ,
5736 } ,
58- ) ;
37+ } ) ;
5938
6039 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?
6367 }
64- } catch {
65- // best effort - response may be undefined?
66- }
6768
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+ }
7476 }
75- }
77+ } ) ;
7678 } ,
7779 } ) ;
7880}
0 commit comments