fix: show friendly error when duplicate category code exists (handle Prisma P2002) (#49)

This commit is contained in:
Artem Sushchev
2025-09-25 12:14:11 +02:00
committed by GitHub
parent 24a2cfc729
commit 07e05aabe7

View File

@@ -188,8 +188,10 @@ export async function addCategoryAction(userId: string, data: Prisma.CategoryCre
return { success: false, error: validatedForm.error.message } return { success: false, error: validatedForm.error.message }
} }
const code = codeFromName(validatedForm.data.name)
try {
const category = await createCategory(userId, { const category = await createCategory(userId, {
code: codeFromName(validatedForm.data.name), code,
name: validatedForm.data.name, name: validatedForm.data.name,
llm_prompt: validatedForm.data.llm_prompt, llm_prompt: validatedForm.data.llm_prompt,
color: validatedForm.data.color || "", color: validatedForm.data.color || "",
@@ -197,6 +199,15 @@ export async function addCategoryAction(userId: string, data: Prisma.CategoryCre
revalidatePath("/settings/categories") 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) { export async function editCategoryAction(userId: string, code: string, data: Prisma.CategoryUpdateInput) {