From 07e05aabe7b5d0d5198f1a6534148a4683d2c1f9 Mon Sep 17 00:00:00 2001 From: Artem Sushchev Date: Thu, 25 Sep 2025 12:14:11 +0200 Subject: [PATCH] fix: show friendly error when duplicate category code exists (handle Prisma P2002) (#49) --- app/(app)/settings/actions.ts | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/app/(app)/settings/actions.ts b/app/(app)/settings/actions.ts index 4907e43..9c41656 100644 --- a/app/(app)/settings/actions.ts +++ b/app/(app)/settings/actions.ts @@ -188,15 +188,26 @@ export async function addCategoryAction(userId: string, data: Prisma.CategoryCre return { success: false, error: validatedForm.error.message } } - const category = await createCategory(userId, { - code: codeFromName(validatedForm.data.name), - name: validatedForm.data.name, - llm_prompt: validatedForm.data.llm_prompt, - color: validatedForm.data.color || "", - }) - revalidatePath("/settings/categories") + const code = codeFromName(validatedForm.data.name) + try { + const category = await createCategory(userId, { + code, + name: validatedForm.data.name, + llm_prompt: validatedForm.data.llm_prompt, + color: validatedForm.data.color || "", + }) + revalidatePath("/settings/categories") - return { success: true, category } + return { success: true, category } + } catch (error: unknown) { + if (error instanceof Prisma.PrismaClientKnownRequestError && error.code === "P2002") { + return { + success: false, + error: `Category with the code "${code}" already exists. Try a different name.`, + } + } + return { success: false, error: "Failed to create category" } + } } export async function editCategoryAction(userId: string, code: string, data: Prisma.CategoryUpdateInput) {