diff --git a/apps/web/app/api/v1/management/responses/lib/response.test.ts b/apps/web/app/api/v1/management/responses/lib/response.test.ts index 33f5b5bf1a..81bb17bf49 100644 --- a/apps/web/app/api/v1/management/responses/lib/response.test.ts +++ b/apps/web/app/api/v1/management/responses/lib/response.test.ts @@ -186,6 +186,18 @@ describe("Response Lib Tests", () => { expect(logger.error).not.toHaveBeenCalled(); // Should be caught and re-thrown as DatabaseError }); + test("should handle RelatedRecordDoesNotExist error with specific message", async () => { + const prismaError = new Prisma.PrismaClientKnownRequestError("Related record does not exist", { + code: "P2025", // PrismaErrorType.RelatedRecordDoesNotExist + clientVersion: "2.0", + }); + vi.mocked(getOrganizationByEnvironmentId).mockResolvedValue(mockOrganization); + vi.mocked(prisma.response.create).mockRejectedValue(prismaError); + + await expect(createResponse(mockResponseInput)).rejects.toThrow(DatabaseError); + await expect(createResponse(mockResponseInput)).rejects.toThrow("Display ID does not exist"); + }); + test("should handle generic errors", async () => { const genericError = new Error("Something went wrong"); vi.mocked(getOrganizationByEnvironmentId).mockResolvedValue(mockOrganization); diff --git a/apps/web/app/api/v1/management/responses/lib/response.ts b/apps/web/app/api/v1/management/responses/lib/response.ts index a7e6fa176a..bfe31c2dc4 100644 --- a/apps/web/app/api/v1/management/responses/lib/response.ts +++ b/apps/web/app/api/v1/management/responses/lib/response.ts @@ -12,6 +12,7 @@ import { validateInputs } from "@/lib/utils/validate"; import { Prisma } from "@prisma/client"; import { cache as reactCache } from "react"; import { prisma } from "@formbricks/database"; +import { PrismaErrorType } from "@formbricks/database/types/error"; import { logger } from "@formbricks/logger"; import { ZId, ZOptionalNumber } from "@formbricks/types/common"; import { TContactAttributes } from "@formbricks/types/contact-attribute"; @@ -176,6 +177,9 @@ export const createResponse = async (responseInput: TResponseInput): Promise