@@ -34,6 +34,10 @@ export type NextApiHandler = (
34
34
) => void | Promise < void > | unknown | Promise < unknown > ;
35
35
export type WrappedNextApiHandler = ( req : NextApiRequest , res : NextApiResponse ) => Promise < void > | Promise < unknown > ;
36
36
37
+ type AugmentedNextApiRequest = NextApiRequest & {
38
+ __withSentry_applied__ ?: boolean ;
39
+ } ;
40
+
37
41
export type AugmentedNextApiResponse = NextApiResponse & {
38
42
__sentryTransaction ?: Transaction ;
39
43
} ;
@@ -42,6 +46,14 @@ export type AugmentedNextApiResponse = NextApiResponse & {
42
46
export const withSentry = ( origHandler : NextApiHandler , parameterizedRoute ?: string ) : WrappedNextApiHandler => {
43
47
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
44
48
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
+
45
57
// first order of business: monkeypatch `res.end()` so that it will wait for us to send events to sentry before it
46
58
// fires (if we don't do this, the lambda will close too early and events will be either delayed or lost)
47
59
// eslint-disable-next-line @typescript-eslint/unbound-method
0 commit comments