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.

Handle Duplicate ExternalId error

Steven Tey233154f325ef
diff --git a/apps/web/app/api/links/[linkId]/route.ts b/apps/web/app/api/links/[linkId]/route.ts
index 2eaf3fe3a1..3e8f198ddd 100644
--- a/apps/web/app/api/links/[linkId]/route.ts
+++ b/apps/web/app/api/links/[linkId]/route.ts
@@ -96,15 +96,29 @@ export const PATCH = withWorkspace(
});
}
- const response = await updateLink({
- oldDomain: link.domain,
- oldKey: link.key,
- updatedLink: processedLink,
- });
+ try {
+ const response = await updateLink({
+ oldDomain: link.domain,
+ oldKey: link.key,
+ updatedLink: processedLink,
+ });
- return NextResponse.json(response, {
- headers,
- });
+ return NextResponse.json(response, {
+ headers,
+ });
+ } catch (error) {
+ if (error.code === "P2002") {
+ throw new DubApiError({
+ code: "conflict",
+ message: "A link with this externalId already exists.",
+ });
+ }
+
+ throw new DubApiError({
+ code: "unprocessable_entity",
+ message: error.message,
+ });
+ }
},
);
diff --git a/apps/web/app/api/links/route.ts b/apps/web/app/api/links/route.ts
index 5161076098..02f54f229a 100644
--- a/apps/web/app/api/links/route.ts
+++ b/apps/web/app/api/links/route.ts
@@ -78,9 +78,22 @@ export const POST = withWorkspace(
});
}
- const response = await createLink(link);
+ try {
+ const response = await createLink(link);
+ return NextResponse.json(response, { headers });
+ } catch (error) {
+ if (error.code === "P2002") {
+ throw new DubApiError({
+ code: "conflict",
+ message: "A link with this externalId already exists.",
+ });
+ }
- return NextResponse.json(response, { headers });
+ throw new DubApiError({
+ code: "unprocessable_entity",
+ message: error.message,
+ });
+ }
},
{
needNotExceededLinks: true,