Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

return error code when you try to create a duplicate tag

Steven Tey533944e5de21
diff --git a/apps/web/app/api/tags/route.ts b/apps/web/app/api/tags/route.ts
index 74f57bc808..e551957008 100644
--- a/apps/web/app/api/tags/route.ts
+++ b/apps/web/app/api/tags/route.ts
@@ -42,15 +42,24 @@ export const POST = withAuth(async ({ req, project, headers }) => {
);
}
const { tag, color } = await req.json();
- const response = await prisma.tag.create({
- data: {
- name: tag,
- color:
- color && COLORS_LIST.map(({ color }) => color).includes(color)
- ? color
- : randomBadgeColor(),
- projectId: project.id,
- },
- });
- return NextResponse.json(response, { headers });
+ try {
+ const response = await prisma.tag.create({
+ data: {
+ name: tag,
+ color:
+ color && COLORS_LIST.map(({ color }) => color).includes(color)
+ ? color
+ : randomBadgeColor(),
+ projectId: project.id,
+ },
+ });
+ return NextResponse.json(response, { headers });
+ } catch (error) {
+ if (error.code === "P2002") {
+ return new Response("A tag with the same name already exists.", {
+ status: 409,
+ });
+ }
+ return new Response(error.message, { status: 400 });
+ }
});