8000 fix(nextjs): Check for validity of API route handler signature (#8811) · vlad-zhukov/sentry-javascript@e6e6ebf · GitHub
[go: up one dir, main page]

Skip to content

Commit e6e6ebf

Browse files
authored
fix(nextjs): Check for validity of API route handler signature (getsentry#8811)
1 parent d1e0135 commit e6e6ebf

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

packages/nextjs/src/common/wrapApiHandlerWithSentry.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,25 @@ export const withSentryAPI = wrapApiHandlerWithSentry;
5353
*/
5454
export function withSentry(apiHandler: NextApiHandler, parameterizedRoute?: string): NextApiHandler {
5555
return new Proxy(apiHandler, {
56-
apply: (wrappingTarget, thisArg, args: [AugmentedNextApiRequest, AugmentedNextApiResponse]) => {
56+
apply: (
57+
wrappingTarget,
58+
thisArg,
59+
args: [AugmentedNextApiRequest | undefined, AugmentedNextApiResponse | undefined],
60+
) => {
5761
const [req, res] = args;
5862

63+
if (!req) {
64+
logger.debug(
65+
`Wrapped API handler on route "${parameterizedRoute}" was not passed a request object. Will not instrument.`,
66+
);
67+
return wrappingTarget.apply(thisArg, args);
68+
} else if (!res) {
69+
logger.debug(
70+
`Wrapped API handler on route "${parameterizedRoute}" was not passed a response object. Will not instrument.`,
71+
);
72+
return wrappingTarget.apply(thisArg, args);
73+
}
74+
5975
// We're now auto-wrapping API route handlers using `wrapApiHandlerWithSentry` (which uses `withSentry` under the hood), but
6076
// users still may have their routes manually wrapped with `withSentry`. This check makes `sentryWrappedHandler`
6177
// idempotent so that those cases don't break anything.

0 commit comments

Comments
 (0)
0