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>
This commit is contained in:
Joe Au-YeungandDevin AI authored and GitHub committed 2026-01-19 18:14:13 +00:00
1 parent 7f46ce1851
commit 2608e136cb
3 files changed
+41 -7

No files matched your search

@@ -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 () => {
@@ -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));
}
@@ -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}`,