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.

fix response json error

Steven Tey16653e1a52f9
diff --git a/apps/web/app/api/workspaces/[idOrSlug]/import/bitly/route.ts b/apps/web/app/api/workspaces/[idOrSlug]/import/bitly/route.ts
index c57ecaf990..f9935dd9fa 100644
--- a/apps/web/app/api/workspaces/[idOrSlug]/import/bitly/route.ts
+++ b/apps/web/app/api/workspaces/[idOrSlug]/import/bitly/route.ts
@@ -1,4 +1,5 @@
import { addDomainToVercel } from "@/lib/api/domains";
+import { DubApiError } from "@/lib/api/errors";
import { bulkCreateLinks } from "@/lib/api/links";
import { createId } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
@@ -13,7 +14,10 @@ import { NextResponse } from "next/server";
export const GET = withWorkspace(async ({ workspace }) => {
const accessToken = await redis.get(`import:bitly:${workspace.id}`);
if (!accessToken) {
- return new Response("No Bitly access token found", { status: 400 });
+ throw new DubApiError({
+ code: "bad_request",
+ message: "No Bitly access token found",
+ });
}
const response = await fetch(`https://api-ssl.bitly.com/v4/groups`, {
headers: {
@@ -23,7 +27,10 @@ export const GET = withWorkspace(async ({ workspace }) => {
});
const data = await response.json();
if (data.message === "FORBIDDEN") {
- return new Response("Invalid Bitly access token", { status: 403 });
+ throw new DubApiError({
+ code: "unauthorized",
+ message: "Invalid Bitly access token",
+ });
}
const groups = data.groups
diff --git a/apps/web/app/api/workspaces/[idOrSlug]/import/rebrandly/route.ts b/apps/web/app/api/workspaces/[idOrSlug]/import/rebrandly/route.ts
index 43cb9b6a4c..2cb55ae4b7 100644
--- a/apps/web/app/api/workspaces/[idOrSlug]/import/rebrandly/route.ts
+++ b/apps/web/app/api/workspaces/[idOrSlug]/import/rebrandly/route.ts
@@ -1,4 +1,5 @@
import { addDomainToVercel } from "@/lib/api/domains";
+import { DubApiError } from "@/lib/api/errors";
import { bulkCreateLinks } from "@/lib/api/links";
import { createId } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
@@ -12,7 +13,10 @@ import { NextResponse } from "next/server";
export const GET = withWorkspace(async ({ workspace }) => {
const accessToken = await redis.get(`import:rebrandly:${workspace.id}`);
if (!accessToken) {
- return new Response("No Rebrandly access token found", { status: 400 });
+ throw new DubApiError({
+ code: "bad_request",
+ message: "No Rebrandly access token found",
+ });
}
const response = await fetch(`https://api.rebrandly.com/v1/domains`, {
@@ -21,10 +25,22 @@ export const GET = withWorkspace(async ({ workspace }) => {
apikey: accessToken as string,
},
});
- const data = await response.json();
- if (data.error === "Unauthorized") {
- return new Response("Invalid Rebrandly access token", { status: 403 });
+ if (!response.ok) {
+ const error = await response.text();
+ if (error === "Unauthorized") {
+ // delete the access token
+ await redis.del(`import:rebrandly:${workspace.id}`);
+ throw new DubApiError({
+ code: "unauthorized",
+ message: "Invalid Rebrandly access token",
+ });
+ }
+ throw new DubApiError({
+ code: "bad_request",
+ message: error,
+ });
}
+ const data = await response.json();
const domains = await Promise.all(
data
diff --git a/apps/web/app/api/workspaces/[idOrSlug]/import/short/route.ts b/apps/web/app/api/workspaces/[idOrSlug]/import/short/route.ts
index 9403cab5bb..80799816f2 100644
--- a/apps/web/app/api/workspaces/[idOrSlug]/import/short/route.ts
+++ b/apps/web/app/api/workspaces/[idOrSlug]/import/short/route.ts
@@ -1,4 +1,5 @@
import { addDomainToVercel } from "@/lib/api/domains";
+import { DubApiError } from "@/lib/api/errors";
import { bulkCreateLinks } from "@/lib/api/links";
import { createId } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
@@ -12,7 +13,10 @@ import { NextResponse } from "next/server";
export const GET = withWorkspace(async ({ workspace }) => {
const accessToken = await redis.get(`import:short:${workspace.id}`);
if (!accessToken) {
- return new Response("No Short.io access token found", { status: 400 });
+ throw new DubApiError({
+ code: "bad_request",
+ message: "No Short.io access token found",
+ });
}
const response = await fetch(`https://api.short.io/api/domains`, {
@@ -23,7 +27,12 @@ export const GET = withWorkspace(async ({ workspace }) => {
});
const data = await response.json();
if (data.error === "Unauthorized") {
- return new Response("Invalid Short.io access token", { status: 403 });
+ // delete the access token
+ await redis.del(`import:short:${workspace.id}`);
+ throw new DubApiError({
+ code: "unauthorized",
+ message: "Invalid Short.io access token",
+ });
}
const domains = await Promise.all(
diff --git a/apps/web/ui/analytics/analytics-provider.tsx b/apps/web/ui/analytics/analytics-provider.tsx
index 15936dc3bb..1617e97d0c 100644
--- a/apps/web/ui/analytics/analytics-provider.tsx
+++ b/apps/web/ui/analytics/analytics-provider.tsx
@@ -298,7 +298,7 @@ export default function AnalyticsProvider({
onSuccess: () => setRequiresUpgrade(false),
onError: (error) => {
try {
- const errorMessage = JSON.parse(error.message)?.error.message;
+ const errorMessage = error.message;
if (
error.status === 403 &&
errorMessage.toLowerCase().includes("upgrade")
diff --git a/apps/web/ui/modals/link-builder/tag-select.tsx b/apps/web/ui/modals/link-builder/tag-select.tsx
index 7a39d0f7c6..be04f8abf6 100644
--- a/apps/web/ui/modals/link-builder/tag-select.tsx
+++ b/apps/web/ui/modals/link-builder/tag-select.tsx
@@ -107,9 +107,6 @@ export function TagSelect() {
body: {
model: "claude-3-haiku-20240307",
},
- onError: (error) => {
- toast.error(error.message);
- },
onFinish: (_, completion) => {
mutateWorkspace();
if (completion) {
diff --git a/packages/utils/src/functions/fetcher.ts b/packages/utils/src/functions/fetcher.ts
index af81e9f06a..68dede8716 100644
--- a/packages/utils/src/functions/fetcher.ts
+++ b/packages/utils/src/functions/fetcher.ts
@@ -10,11 +10,11 @@ export async function fetcher<JSON = any>(
const res = await fetch(input, init);
if (!res.ok) {
- const error = new Error(
- "An error occurred while fetching the data.",
- ) as SWRError;
-
- error.info = (await res.json()).error;
+ const message =
+ (await res.json())?.error?.message ||
+ "An error occurred while fetching the data.";
+ const error = new Error(message) as SWRError;
+ error.info = message;
error.status = res.status;
throw error;