@@ -34,6 +34,10 @@ export type NextApiHandler = (
3434) => void | Promise < void > | unknown | Promise < unknown > ;
3535export type WrappedNextApiHandler = ( req : NextApiRequest , res : NextApiResponse ) => Promise < void > | Promise < unknown > ;
3636
37+ type AugmentedNextApiRequest = NextApiRequest & {
38+ __withSentry_applied__ ?: boolean ;
39+ } ;
40+
3741export type AugmentedNextApiResponse = NextApiResponse & {
3842 __sentryTransaction ?: Transaction ;
3943} ;
@@ -42,6 +46,14 @@ export type AugmentedNextApiResponse = NextApiResponse & {
4246export 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