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 error handling and add error messages

Kiran Kefbd99732e89
diff --git a/apps/web/app/api/links/info/route.ts b/apps/web/app/api/links/info/route.ts
index e6f191b12e..c4c57a4746 100644
--- a/apps/web/app/api/links/info/route.ts
+++ b/apps/web/app/api/links/info/route.ts
@@ -1,5 +1,5 @@
import { withAuth } from "@/lib/auth";
-import { handleAndReturnErrorResponse } from "@/lib/errors";
+import { DubApiError, handleAndReturnErrorResponse } from "@/lib/errors";
import prisma from "@/lib/prisma";
import { getLinkInfoQuerySchema } from "@/lib/zod/schemas/links";
import { linkConstructor } from "@dub/utils";
@@ -10,7 +10,7 @@ export const GET = withAuth(async ({ headers, searchParams }) => {
try {
const { domain, key } = getLinkInfoQuerySchema.parse(searchParams);
- const response = await prisma.link.findUniqueOrThrow({
+ const response = await prisma.link.findUnique({
where: {
domain_key: {
domain,
@@ -22,6 +22,13 @@ export const GET = withAuth(async ({ headers, searchParams }) => {
},
});
+ if (!response) {
+ throw new DubApiError({
+ code: "not_found",
+ message: "Link not found.",
+ });
+ }
+
return NextResponse.json(
{
...response,
diff --git a/apps/web/app/api/projects/[slug]/users/route.ts b/apps/web/app/api/projects/[slug]/users/route.ts
index ec1b3c9020..1e788fbce7 100644
--- a/apps/web/app/api/projects/[slug]/users/route.ts
+++ b/apps/web/app/api/projects/[slug]/users/route.ts
@@ -77,7 +77,7 @@ export const DELETE = withAuth(
async ({ searchParams, project }) => {
try {
const { userId } = removeUserSchema.parse(searchParams);
- const projectUser = await prisma.projectUsers.findUniqueOrThrow({
+ const projectUser = await prisma.projectUsers.findUnique({
where: {
userId_projectId: {
projectId: project.id,
@@ -88,6 +88,12 @@ export const DELETE = withAuth(
role: true,
},
});
+ if (!projectUser) {
+ throw new DubApiError({
+ code: "not_found",
+ message: "User not found",
+ });
+ }
if (projectUser.role === "owner") {
throw new DubApiError({
code: "bad_request",
diff --git a/apps/web/lib/errors/index.ts b/apps/web/lib/errors/index.ts
index 3cf6c0a73e..cc23f42ce9 100644
--- a/apps/web/lib/errors/index.ts
+++ b/apps/web/lib/errors/index.ts
@@ -1,4 +1,3 @@
-// import { PrismaClientKnownRequestError } from "@prisma/client/runtime/library";
import { NextResponse } from "next/server";
import { ZodError } from "zod";
import { generateErrorMessage } from "zod-error";
@@ -33,14 +32,6 @@ const errorCodeToHttpStatus: Record<z.infer<typeof ErrorCode>, number> = {
internal_server_error: 500,
};
-// const prismaErrorMapping: Record<
-// PrismaClientKnownRequestError["code"],
-// ErrorResponse["error"]["code"]
-// > = {
-// P2025: "not_found",
-// P2002: "conflict",
-// };
-
const ErrorSchema = z.object({
error: z.object({
code: ErrorCode.openapi({
@@ -111,27 +102,11 @@ export function fromZodError(error: ZodError): ErrorResponse {
}
export function handleApiError(error: any): ErrorResponse & { status: number } {
- console.error(error);
-
// Zod errors
if (error instanceof ZodError) {
return { ...fromZodError(error), status: 400 };
}
- // Prisma errors
- // if (error instanceof PrismaClientKnownRequestError) {
- // const code = prismaErrorMapping[error.code] ?? error.message;
-
- // return {
- // error: {
- // code,
- // message: error.message,
- // doc_url: `${docErrorUrl}#${code}`,
- // },
- // status: errorCodeToHttpStatus[code],
- // };
- // }
-
// DubApiError errors
if (error instanceof DubApiError) {
return {