@@ -4,11 +4,11 @@ import {
44 SEMANTIC_ATTRIBUTE_SENTRY_SOURCE ,
55 SPAN_STATUS_ERROR ,
66 captureException ,
7- getCurrentScope ,
87 handleCallbackErrors ,
98 setHttpStatus ,
109 startSpan ,
1110 withIsolationScope ,
11+ withScope ,
1212} from '@sentry/core' ;
1313import { propagationContextFromHeaders , winterCGHeadersToDict } from '@sentry/utils' ;
1414import { isNotFoundNavigationError , isRedirectNavigationError } from './nextNavigationErrorUtils' ;
@@ -51,57 +51,59 @@ export function wrapRouteHandlerWithSentry<F extends (...args: any[]) => any>(
5151
5252 const propagationContext = commonObjectToPropagationContext ( headers , incomingPropagationContext ) ;
5353
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' ,
8466 } ,
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+ ) ;
8687
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?
9094 }
91- } catch {
92- // best effort - response may be undefined?
93- }
9495
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+ }
103105 }
104- }
106+ } ) ;
105107 } ) ;
106108 } ) ;
107109 } ,
0 commit comments