Skip to content

Commit

Permalink
fix(nextjs): Check for validity of API route handler signature (#8811)
Browse files Browse the repository at this point in the history
  • Loading branch information
lforst committed Aug 14, 2023
1 parent d1e0135 commit e6e6ebf
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion packages/nextjs/src/common/wrapApiHandlerWithSentry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,25 @@ export const withSentryAPI = wrapApiHandlerWithSentry;
*/
export function withSentry(apiHandler: NextApiHandler, parameterizedRoute?: string): NextApiHandler {
return new Proxy(apiHandler, {
apply: (wrappingTarget, thisArg, args: [AugmentedNextApiRequest, AugmentedNextApiResponse]) => {
apply: (
wrappingTarget,
thisArg,
args: [AugmentedNextApiRequest | undefined, AugmentedNextApiResponse | undefined],
) => {
const [req, res] = args;

if (!req) {
logger.debug(
`Wrapped API handler on route "${parameterizedRoute}" was not passed a request object. Will not instrument.`,
);
return wrappingTarget.apply(thisArg, args);
} else if (!res) {
logger.debug(
`Wrapped API handler on route "${parameterizedRoute}" was not passed a response object. Will not instrument.`,
);
return wrappingTarget.apply(thisArg, args);
}

// We're now auto-wrapping API route handlers using `wrapApiHandlerWithSentry` (which uses `withSentry` under the hood), but
// users still may have their routes manually wrapped with `withSentry`. This check makes `sentryWrappedHandler`
// idempotent so that those cases don't break anything.
Expand Down

0 comments on commit e6e6ebf

Please sign in to comment.