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