@@ -10,9 +10,9 @@ import { consoleSandbox, isString, logger, objectify } from '@sentry/utils';
1010
1111import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from '@sentry/core' ;
1212import type { AugmentedNextApiRequest , AugmentedNextApiResponse , NextApiHandler } from './types' ;
13- import { platformSupportsStreaming } from './utils/platformSupportsStreaming' ;
14- import { flushQueue } from './utils/responseEnd' ;
13+ import { flushSafelyWithTimeout } from './utils/responseEnd' ;
1514import { escapeNextjsTracing } from './utils/tracingUtils' ;
15+ import { vercelWaitUntil } from './utils/vercelWaitUntil' ;
1616
1717/**
1818 * 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
8383 apply ( target , thisArg , argArray ) {
8484 setHttpStatus ( span , res . statusCode ) ;
8585 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 ) ;
9588 } ,
9689 } ) ;
9790
@@ -138,14 +131,7 @@ export function wrapApiHandlerWithSentry(apiHandler: NextApiHandler, parameteriz
138131 setHttpStatus ( span , res . statusCode ) ;
139132 span . end ( ) ;
140133
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 ( ) ) ;
149135
150136 // We rethrow here so that nextjs can do with the error whatever it would normally do. (Sometimes "whatever it
151137 // would normally do" is to allow the error to bubble up to the global handlers - another reason we need to mark
0 commit comments