@@ -176,31 +176,7 @@ type WrappedResponseEndMethod = AugmentedNextApiResponse['end'];
176
176
*/
177
177
function wrapEndMethod ( origEnd : ResponseEndMethod ) : WrappedResponseEndMethod {
178
178
return async function newEnd ( this : AugmentedNextApiResponse , ...args : unknown [ ] ) {
179
- const transaction = this . __sentryTransaction ;
180
-
181
- if ( transaction ) {
182
- transaction . setHttpStatus ( this . statusCode ) ;
183
-
184
- // Push `transaction.finish` to the next event loop so open spans have a better chance of finishing before the
185
- // transaction closes, and make sure to wait until that's done before flushing events
186
- const transactionFinished : Promise < void > = new Promise ( resolve => {
187
- setImmediate ( ( ) => {
188
- transaction . finish ( ) ;
189
- resolve ( ) ;
190
- } ) ;
191
- } ) ;
192
- await transactionFinished ;
193
- }
194
-
195
- // flush the event queue to ensure that events get sent to Sentry before the response is finished and the lambda
196
- // ends
197
- try {
198
- logger . log ( 'Flushing events...' ) ;
199
- await flush ( 2000 ) ;
200
- logger . log ( 'Done flushing events' ) ;
201
- } catch ( e ) {
202
- logger . log ( `Error while flushing events:\n${ e } ` ) ;
203
- }
179
+ await finishSentryProcessing ( this ) ;
204
180
205
181
// If the request didn't error, we will have temporarily marked the response finished to avoid a nextjs warning
206
182
// message. (See long note above.) Now we need to flip `finished` back to `false` so that the real `res.end()`
@@ -210,3 +186,36 @@ function wrapEndMethod(origEnd: ResponseEndMethod): WrappedResponseEndMethod {
210
186
return origEnd . call ( this , ...args ) ;
211
187
} ;
212
188
}
189
+
190
+ /**
191
+ * Close the open transaction (if any) and flush events to Sentry.
192
+ *
193
+ * @param res The outgoing response for this request, on which the transaction is stored
194
+ */
195
+ async function finishSentryProcessing ( res : AugmentedNextApiResponse ) : Promise < void > {
196
+ const { __sentryTransaction : transaction } = res ;
197
+
198
+ if ( transaction ) {
199
+ transaction . setHttpStatus ( res . statusCode ) ;
200
+
201
+ // Push `transaction.finish` to the next event loop so open spans have a better chance of finishing before the
202
+ // transaction closes, and make sure to wait until that's done before flushing events
203
+ const transactionFinished : Promise < void > = new Promise ( resolve => {
204
+ setImmediate ( ( ) => {
205
+ transaction . finish ( ) ;
206
+ resolve ( ) ;
207
+ } ) ;
208
+ } ) ;
209
+ await transactionFinished ;
210
+ }
211
+
212
+ // Flush the event queue to ensure that events get sent to Sentry before the response is finished and the lambda
213
+ // ends. If there was an error, rethrow it so that the normal exception-handling mechanisms can apply.
214
+ try {
215
+ logger . log ( 'Flushing events...' ) ;
216
+ await flush ( 2000 ) ;
217
+ logger . log ( 'Done flushing events' ) ;
218
+ } catch ( e ) {
219
+ logger . log ( `Error while flushing events:\n${ e } ` ) ;
220
+ }
221
+ }
0 commit comments