Skip to content

Commit 4902789

Browse files
committed
make withSentry's returned wrapper idempotent
1 parent e11aed4 commit 4902789

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

packages/nextjs/src/utils/withSentry.ts

+12
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ export type NextApiHandler = (
3434
) => void | Promise<void> | unknown | Promise<unknown>;
3535
export type WrappedNextApiHandler = (req: NextApiRequest, res: NextApiResponse) => Promise<void> | Promise<unknown>;
3636

37+
type AugmentedNextApiRequest = NextApiRequest & {
38+
__withSentry_applied__?: boolean;
39+
};
40+
3741
export type AugmentedNextApiResponse = NextApiResponse & {
3842
__sentryTransaction?: Transaction;
3943
};
@@ -42,6 +46,14 @@ export type AugmentedNextApiResponse = NextApiResponse & {
4246
export const withSentry = (origHandler: NextApiHandler, parameterizedRoute?: string): WrappedNextApiHandler => {
4347
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
4448
return async (req, res) => {
49+
// We're now auto-wrapping API route handlers using `withSentryAPI` (which uses `withSentry` under the hood), but
50+
// users still may have their routes manually wrapped with `withSentry`. This check makes `sentryWrappedHandler`
51+
// idempotent so that those cases don't break anything.
52+
if (req.__withSentry_applied__) {
53+
return origHandler(req, res);
54+
}
55+
req.__withSentry_applied__ = true;
56+
4557
// first order of business: monkeypatch `res.end()` so that it will wait for us to send events to sentry before it
4658
// fires (if we don't do this, the lambda will close too early and events will be either delayed or lost)
4759
// eslint-disable-next-line @typescript-eslint/unbound-method

0 commit comments

Comments
 (0)