Skip to content

Scheduling infrastructure for absolutely everyone.

fix: use appropriate error codes in chargeCard handler (#27013)

Joe Au-Yeung2608e136cbbc
diff --git a/packages/features/bookings/lib/payment/handleNoShowFee.test.ts b/packages/features/bookings/lib/payment/handleNoShowFee.test.ts
index f7c3bddab89..6d0ff342f51 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 f4eddba1ea8..67892f7dff7 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 359ef8355dd..184060f22bc 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}`,