Skip to content

Commit c7de9c3

Browse files
committed
await flush in api handlers
1 parent ab28544 commit c7de9c3

File tree

1 file changed

+4
-12
lines changed

1 file changed

+4
-12
lines changed

packages/nextjs/src/common/wrapApiHandlerWithSentry.ts

+4-12
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
withIsolationScope,
88
} from '@sentry/core';
99
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from '@sentry/core';
10-
import { isString, logger, objectify, vercelWaitUntil } from '@sentry/utils';
10+
import { isString, logger, objectify } from '@sentry/utils';
1111
import type { NextApiRequest } from 'next';
1212
import type { AugmentedNextApiResponse, NextApiHandler } from './types';
1313
import { flushSafelyWithTimeout } from './utils/responseEnd';
@@ -77,16 +77,6 @@ export function wrapApiHandlerWithSentry(apiHandler: NextApiHandler, parameteriz
7777
},
7878
},
7979
async span => {
80-
// eslint-disable-next-line @typescript-eslint/unbound-method
81-
res.end = new Proxy(res.end, {
82-
apply(target, thisArg, argArray) {
83-
setHttpStatus(span, res.statusCode);
84-
span.end();
85-
vercelWaitUntil(flushSafelyWithTimeout());
86-
return target.apply(thisArg, argArray);
87-
},
88-
});
89-
9080
try {
9181
return await wrappingTarget.apply(thisArg, args);
9282
} catch (e) {
@@ -110,7 +100,9 @@ export function wrapApiHandlerWithSentry(apiHandler: NextApiHandler, parameteriz
110100
setHttpStatus(span, 500);
111101
span.end();
112102

113-
vercelWaitUntil(flushSafelyWithTimeout());
103+
// we need to await the flush here to ensure that the error is captured
104+
// as the runtime freezes as soon as the error is thrown below
105+
await flushSafelyWithTimeout();
114106

115107
// We rethrow here so that nextjs can do with the error whatever it would normally do. (Sometimes "whatever it
116108
// would normally do" is to allow the error to bubble up to the global handlers - another reason we need to mark

0 commit comments

Comments
 (0)