From 53e32a5bbad43b262a5e1fef37f57fc67738afdb Mon Sep 17 00:00:00 2001 From: Chayan Das <145531182+ChayanDass@users.noreply.github.com> Date: Wed, 1 Jul 2026 01:26:48 +0530 Subject: [PATCH] fix: return 404 when booking does not exist (#29583) Signed-off-by: Chayan Das Co-authored-by: Bandhan Majumder <133476557+bandhan-majumder@users.noreply.github.com> --- .../features/bookings/lib/handleCancelBooking.ts | 15 ++++++++++++++- .../test/handleCancelBooking.test.ts | 14 ++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/packages/features/bookings/lib/handleCancelBooking.ts b/packages/features/bookings/lib/handleCancelBooking.ts index 736ffdf56b..c8f2df2e86 100644 --- a/packages/features/bookings/lib/handleCancelBooking.ts +++ b/packages/features/bookings/lib/handleCancelBooking.ts @@ -49,6 +49,7 @@ import { getAllCredentialsIncludeServiceAccountKey } from "./getAllCredentialsFo import { getBookingToDelete } from "./getBookingToDelete"; import cancelAttendeeSeat from "./handleSeats/cancel/cancelAttendeeSeat"; import type { IBookingCancelService } from "./interfaces/IBookingCancelService"; +import { isPrismaError } from "@calcom/lib/server/getServerErrorFromUnknown"; const log = logger.getSubLogger({ prefix: ["handleCancelBooking"] }); @@ -102,7 +103,19 @@ async function handler(input: CancelBookingInput, dependencies?: Dependencies) { skipCancellationReasonValidation = false, skipCalendarSyncTaskCancellation = false, } = bookingCancelInput.parse(body); - const bookingToDelete = await getBookingToDelete(id, uid); + let bookingToDelete: BookingToDelete + try { + bookingToDelete = await getBookingToDelete(id, uid); + } catch (error) { + if (isPrismaError(error) && error.code === "P2025") // Record not found + { + throw new HttpError({ + statusCode: 404, + message: "Booking not found.", + }); + } + throw error; + } const { userId, platformBookingUrl, diff --git a/packages/features/bookings/lib/handleCancelBooking/test/handleCancelBooking.test.ts b/packages/features/bookings/lib/handleCancelBooking/test/handleCancelBooking.test.ts index 3cd18c4a7c..fac503b098 100644 --- a/packages/features/bookings/lib/handleCancelBooking/test/handleCancelBooking.test.ts +++ b/packages/features/bookings/lib/handleCancelBooking/test/handleCancelBooking.test.ts @@ -142,7 +142,21 @@ describe("Cancel Booking", () => { }, }, }); + }); + test("Should throw when cancelling a non-existing booking", async () => { + const handleCancelBooking = (await import("@calcom/features/bookings/lib/handleCancelBooking")).default; + + await expect( + handleCancelBooking({ + bookingData: { + id: 99999, + uid: "non-existing-uid", + cancelledBy: "someone@example.com", + cancellationReason: "No reason", + }, + }) + ).rejects.toThrow("Booking not found."); }); test("Should call processPaymentRefund", async () => {