Skip to content

Commit 68aa490

Browse files
committed
also flush before rethrowing error
1 parent 4702da7 commit 68aa490

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

packages/nextjs/src/utils/withSentry.ts

+6
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@ export const withSentry = (origHandler: NextApiHandler): WrappedNextApiHandler =
150150
captureException(objectifiedErr);
151151
}
152152

153+
// Make sure we have a chance to finish the transaction and flush events to Sentry before the handler errors
154+
// out. (Apps which are deployed on Vercel run their API routes in lambdas, and those lambdas will shut down the
155+
// moment they detect an error, so it's important to get this done before rethrowing the error. Apps not
156+
// deployed serverlessly will run into this cleanup function again in `res.end(), but it'll just no-op.)
157+
await finishSentryProcessing(res);
158+
153159
// We rethrow here so that nextjs can do with the error whatever it would normally do. (Sometimes "whatever it
154160
// would normally do" is to allow the error to bubble up to the global handlers - another reason we need to mark
155161
// the error as already having been captured.)

0 commit comments

Comments
 (0)