From 2608e136cbbcf425a721c1433943a7d07654fab0 Mon Sep 17 00:00:00 2001 From: Joe Au-Yeung <65426560+joeauyeung@users.noreply.github.com> Date: Mon, 19 Jan 2026 13:14:13 -0500 Subject: [PATCH] fix: use appropriate error codes in chargeCard handler (#27013) - Change 'Booking not found' to throw NOT_FOUND (404) instead of plain Error - Preserve ErrorWithCode for ChargeCardFailure to properly map to BAD_REQUEST (400) - Add specific error handling for 'User is not a member of the team' -> FORBIDDEN (403) - Add specific error handling for 'User ID is required' and 'No payment credential found' -> BAD_REQUEST (400) - Keep INTERNAL_SERVER_ERROR (500) only for actual server errors like 'Payment app not implemented' - Update test to expect ErrorWithCode to be re-thrown instead of wrapped in translated Error Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- .../lib/payment/handleNoShowFee.test.ts | 7 ++-- .../bookings/lib/payment/handleNoShowFee.ts | 5 +-- .../viewer/payments/chargeCard.handler.ts | 36 ++++++++++++++++++- 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/packages/features/bookings/lib/payment/handleNoShowFee.test.ts b/packages/features/bookings/lib/payment/handleNoShowFee.test.ts index f7c3bddab8..6d0ff342f5 100644 --- a/packages/features/bookings/lib/payment/handleNoShowFee.test.ts +++ b/packages/features/bookings/lib/payment/handleNoShowFee.test.ts @@ -355,9 +355,8 @@ describe("handleNoShowFee", () => { }); it("should handle ChargeCardFailure error with proper message", async () => { - mockPaymentService.chargeCard.mockRejectedValue( - new ErrorWithCode(ErrorCode.ChargeCardFailure, "Card declined") - ); + const chargeCardError = new ErrorWithCode(ErrorCode.ChargeCardFailure, "Card declined"); + mockPaymentService.chargeCard.mockRejectedValue(chargeCardError); vi.mocked(CredentialRepository.findPaymentCredentialByAppIdAndUserIdOrTeamId).mockResolvedValue( mockCredential @@ -369,7 +368,7 @@ describe("handleNoShowFee", () => { booking: mockBooking, payment: mockPayment, }) - ).rejects.toThrow("Translated: Card declined"); + ).rejects.toThrow(chargeCardError); }); it("should handle generic payment errors", async () => { diff --git a/packages/features/bookings/lib/payment/handleNoShowFee.ts b/packages/features/bookings/lib/payment/handleNoShowFee.ts index f4eddba1ea..67892f7dff 100644 --- a/packages/features/bookings/lib/payment/handleNoShowFee.ts +++ b/packages/features/bookings/lib/payment/handleNoShowFee.ts @@ -165,11 +165,12 @@ export const handleNoShowFee = async ({ return paymentData; } catch (err) { - let errorMessage = `Error processing paymentId ${payment.id} with error ${err}`; if (err instanceof ErrorWithCode && err.code === ErrorCode.ChargeCardFailure) { - errorMessage = err.message; + log.error(err.message); + throw err; } + const errorMessage = `Error processing paymentId ${payment.id} with error ${err}`; log.error(errorMessage); throw new Error(tOrganizer(errorMessage)); } diff --git a/packages/trpc/server/routers/viewer/payments/chargeCard.handler.ts b/packages/trpc/server/routers/viewer/payments/chargeCard.handler.ts index 359ef8355d..184060f22b 100644 --- a/packages/trpc/server/routers/viewer/payments/chargeCard.handler.ts +++ b/packages/trpc/server/routers/viewer/payments/chargeCard.handler.ts @@ -1,5 +1,7 @@ import { handleNoShowFee } from "@calcom/features/bookings/lib/payment/handleNoShowFee"; import { BookingRepository } from "@calcom/features/bookings/repositories/BookingRepository"; +import { ErrorCode } from "@calcom/lib/errorCodes"; +import { ErrorWithCode } from "@calcom/lib/errors"; import type { PrismaClient } from "@calcom/prisma"; import { TRPCError } from "@trpc/server"; @@ -18,7 +20,10 @@ export const chargeCardHandler = async ({ ctx, input }: ChargeCardHandlerOptions const booking = await bookingRepository.getBookingForPaymentProcessing(input.bookingId); if (!booking) { - throw new Error("Booking not found"); + throw new TRPCError({ + code: "NOT_FOUND", + message: "Booking not found", + }); } if (booking.payment[0].success) { @@ -35,6 +40,35 @@ export const chargeCardHandler = async ({ ctx, input }: ChargeCardHandlerOptions }); } catch (error) { console.error(error); + + if (error instanceof ErrorWithCode && error.code === ErrorCode.ChargeCardFailure) { + throw new TRPCError({ + code: "BAD_REQUEST", + message: error.message, + }); + } + + if (error instanceof Error) { + const message = error.message; + + if (message.includes("User is not a member of the team")) { + throw new TRPCError({ + code: "FORBIDDEN", + message, + }); + } + + if ( + message.includes("User ID is required") || + message.includes("No payment credential found") + ) { + throw new TRPCError({ + code: "BAD_REQUEST", + message, + }); + } + } + throw new TRPCError({ code: "INTERNAL_SERVER_ERROR", message: `Failed to charge no show fee for ${booking.id}`,