@@ -10,9 +10,9 @@ import { consoleSandbox, isString, logger, objectify } from '@sentry/utils';
10
10
11
11
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from '@sentry/core' ;
12
12
import type { AugmentedNextApiRequest , AugmentedNextApiResponse , NextApiHandler } from './types' ;
13
- import { platformSupportsStreaming } from './utils/platformSupportsStreaming' ;
14
- import { flushQueue } from './utils/responseEnd' ;
13
+ import { flushSafelyWithTimeout } from './utils/responseEnd' ;
15
14
import { escapeNextjsTracing } from './utils/tracingUtils' ;
15
+ import { vercelWaitUntil } from './utils/vercelWaitUntil' ;
16
16
17
17
/**
18
18
* Wrap the given API route handler for tracing and error capturing. Thin wrapper around `withSentry`, which only
@@ -83,15 +83,8 @@ export function wrapApiHandlerWithSentry(apiHandler: NextApiHandler, parameteriz
83
83
apply ( target , thisArg , argArray ) {
84
84
setHttpStatus ( span , res . statusCode ) ;
85
85
span . end ( ) ;
86
- if ( platformSupportsStreaming ( ) && ! wrappingTarget . __sentry_test_doesnt_support_streaming__ ) {
87
- target . apply ( thisArg , argArray ) ;
88
- } else {
89
- // flushQueue will not reject
90
- // eslint-disable-next-line @typescript-eslint/no-floating-promises
91
- flushQueue ( ) . then ( ( ) => {
92
- target . apply ( thisArg , argArray ) ;
93
- } ) ;
94
- }
86
+ vercelWaitUntil ( flushSafelyWithTimeout ( ) ) ;
87
+ target . apply ( thisArg , argArray ) ;
95
88
} ,
96
89
} ) ;
97
90
@@ -138,14 +131,7 @@ export function wrapApiHandlerWithSentry(apiHandler: NextApiHandler, parameteriz
138
131
setHttpStatus ( span , res . statusCode ) ;
139
132
span . end ( ) ;
140
133
141
- // Make sure we have a chance to finish the transaction and flush events to Sentry before the handler errors
142
- // out. (Apps which are deployed on Vercel run their API routes in lambdas, and those lambdas will shut down the
143
- // moment they detect an error, so it's important to get this done before rethrowing the error. Apps not
144
- // deployed serverlessly will run into this cleanup code again in `res.end(), but the transaction will already
145
- // be finished and the queue will already be empty, so effectively it'll just no-op.)
146
- if ( platformSupportsStreaming ( ) && ! wrappingTarget . __sentry_test_doesnt_support_streaming__ ) {
147
- await flushQueue ( ) ;
148
- }
134
+ vercelWaitUntil ( flushSafelyWithTimeout ( ) ) ;
149
135
150
136
// We rethrow here so that nextjs can do with the error whatever it would normally do. (Sometimes "whatever it
151
137
// would normally do" is to allow the error to bubble up to the global handlers - another reason we need to mark
0 commit comments