Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

fix: return json on errors

Marc Seitzecac8531feb7
diff --git a/pages/api/teams/[teamId]/datarooms/[id]/documents/[documentId]/index.ts b/pages/api/teams/[teamId]/datarooms/[id]/documents/[documentId]/index.ts
index c59e9337..81289402 100644
--- a/pages/api/teams/[teamId]/datarooms/[id]/documents/[documentId]/index.ts
+++ b/pages/api/teams/[teamId]/datarooms/[id]/documents/[documentId]/index.ts
@@ -14,8 +14,7 @@ export default async function handle(
// PATCH /api/teams/:teamId/datarooms/:id/documents/:documentId
const session = await getServerSession(req, res, authOptions);
if (!session) {
- res.status(401).end("Unauthorized");
- return;
+ return res.status(401).json({ message: "Unauthorized" });
}
const userId = (session.user as CustomUser).id;
const {
@@ -42,7 +41,7 @@ export default async function handle(
});
if (!team) {
- return res.status(401).end("Unauthorized");
+ return res.status(401).json({ message: "Unauthorized" });
}
const document = await prisma.dataroomDocument.update({
@@ -63,7 +62,7 @@ export default async function handle(
});
if (!document) {
- return res.status(404).end("Document not found");
+ return res.status(404).json({ message: "Document not found" });
}
return res.status(200).json({
@@ -76,8 +75,7 @@ export default async function handle(
/// DELETE /api/teams/:teamId/datarooms/:id/documents/:documentId
const session = await getServerSession(req, res, authOptions);
if (!session) {
- res.status(401).end("Unauthorized");
- return;
+ return res.status(401).json({ message: "Unauthorized" });
}
const userId = (session.user as CustomUser).id;
@@ -100,7 +98,7 @@ export default async function handle(
},
});
if (!teamAccess) {
- return res.status(401).end("Unauthorized");
+ return res.status(401).json({ message: "Unauthorized" });
}
if (teamAccess.role !== "ADMIN" && teamAccess.role !== "MANAGER") {
@@ -118,7 +116,7 @@ export default async function handle(
});
if (!dataroom) {
- return res.status(401).end("Dataroom not found");
+ return res.status(404).json({ message: "Dataroom not found" });
}
const document = await prisma.dataroomDocument.delete({
@@ -129,7 +127,7 @@ export default async function handle(
});
if (!document) {
- return res.status(404).end("Document not found");
+ return res.status(404).json({ message: "Document not found" });
}
return res.status(204).end(); // No Content
@@ -137,6 +135,8 @@ export default async function handle(
} else {
// We only allow PATCH and DELETE requests
res.setHeader("Allow", ["PATCH", "DELETE"]);
- return res.status(405).end(`Method ${req.method} Not Allowed`);
+ return res
+ .status(405)
+ .json({ message: `Method ${req.method} Not Allowed` });
}
}
diff --git a/pages/api/teams/[teamId]/datarooms/[id]/folders/manage/[folderId]/index.ts b/pages/api/teams/[teamId]/datarooms/[id]/folders/manage/[folderId]/index.ts
index 79742496..8cb2bada 100644
--- a/pages/api/teams/[teamId]/datarooms/[id]/folders/manage/[folderId]/index.ts
+++ b/pages/api/teams/[teamId]/datarooms/[id]/folders/manage/[folderId]/index.ts
@@ -16,8 +16,7 @@ export default async function handle(
// DELETE /api/teams/:teamId/datarooms/[:id]/folders/manage
const session = await getServerSession(req, res, authOptions);
if (!session) {
- res.status(401).end("Unauthorized");
- return;
+ return res.status(401).json({ message: "Unauthorized" });
}
const {
@@ -46,7 +45,7 @@ export default async function handle(
});
if (!teamAccess) {
- return res.status(401).end("Unauthorized");
+ return res.status(401).json({ message: "Unauthorized" });
}
if (teamAccess.role !== "ADMIN" && teamAccess.role !== "MANAGER") {
@@ -64,7 +63,7 @@ export default async function handle(
});
if (!dataroom) {
- return res.status(401).end("Dataroom not found");
+ return res.status(404).json({ message: "Dataroom not found" });
}
const folder = await prisma.dataroomFolder.findUnique({
@@ -90,7 +89,9 @@ export default async function handle(
} else {
// We only allow DELETE requests
res.setHeader("Allow", ["DELETE"]);
- return res.status(405).end(`Method ${req.method} Not Allowed`);
+ return res
+ .status(405)
+ .json({ message: `Method ${req.method} Not Allowed` });
}
}
diff --git a/pages/api/teams/[teamId]/documents/[id]/index.ts b/pages/api/teams/[teamId]/documents/[id]/index.ts
index 2799c178..fa62b17d 100644
--- a/pages/api/teams/[teamId]/documents/[id]/index.ts
+++ b/pages/api/teams/[teamId]/documents/[id]/index.ts
@@ -19,7 +19,7 @@ export default async function handle(
// GET /api/teams/:teamId/documents/:id
const session = await getServerSession(req, res, authOptions);
if (!session) {
- return res.status(401).end("Unauthorized");
+ return res.status(401).json({ message: "Unauthorized" });
}
const { teamId, id: docId } = req.query as { teamId: string; id: string };
@@ -53,7 +53,7 @@ export default async function handle(
});
if (!teamAccess) {
- return res.status(401).end("Unauthorized");
+ return res.status(401).json({ message: "Unauthorized" });
}
// Then fetch the specific document with its relationships (targeted query)
@@ -139,8 +139,7 @@ export default async function handle(
// PUT /api/teams/:teamId/document/:id
const session = await getServerSession(req, res, authOptions);
if (!session) {
- res.status(401).end("Unauthorized");
- return;
+ return res.status(401).json({ message: "Unauthorized" });
}
const userId = (session.user as CustomUser).id;
const { teamId, id: docId } = req.query as { teamId: string; id: string };
@@ -175,7 +174,7 @@ export default async function handle(
});
if (!document) {
- return res.status(404).end("Document not found");
+ return res.status(404).json({ message: "Document not found" });
}
return res.status(200).json({
@@ -187,8 +186,7 @@ export default async function handle(
// DELETE /api/teams/:teamId/document/:id
const session = await getServerSession(req, res, authOptions);
if (!session) {
- res.status(401).end("Unauthorized");
- return;
+ return res.status(401).json({ message: "Unauthorized" });
}
const { teamId, id: docId } = req.query as { teamId: string; id: string };
@@ -208,7 +206,7 @@ export default async function handle(
},
});
if (!teamAccess) {
- return res.status(401).end("Unauthorized");
+ return res.status(401).json({ message: "Unauthorized" });
}
if (teamAccess.role !== "ADMIN" && teamAccess.role !== "MANAGER") {
@@ -236,7 +234,7 @@ export default async function handle(
});
if (!documentVersions) {
- return res.status(404).end("Document not found");
+ return res.status(404).json({ message: "Document not found" });
}
//if it is not notion document then only delete the document from storage
@@ -265,6 +263,8 @@ export default async function handle(
} else {
// We only allow GET, PUT and DELETE requests
res.setHeader("Allow", ["GET", "PUT", "DELETE"]);
- return res.status(405).end(`Method ${req.method} Not Allowed`);
+ return res
+ .status(405)
+ .json({ message: `Method ${req.method} Not Allowed` });
}
}
diff --git a/pages/api/teams/[teamId]/folders/manage/[folderId]/index.ts b/pages/api/teams/[teamId]/folders/manage/[folderId]/index.ts
index 9a28cf07..4ad4d206 100644
--- a/pages/api/teams/[teamId]/folders/manage/[folderId]/index.ts
+++ b/pages/api/teams/[teamId]/folders/manage/[folderId]/index.ts
@@ -16,8 +16,7 @@ export default async function handle(
// DELETE /api/teams/:teamId/folders/manage/:folderId
const session = await getServerSession(req, res, authOptions);
if (!session) {
- res.status(401).end("Unauthorized");
- return;
+ return res.status(401).json({ message: "Unauthorized" });
}
const { teamId, folderId } = req.query as {
@@ -41,7 +40,7 @@ export default async function handle(
});
if (!teamAccess) {
- return res.status(401).end("Unauthorized");
+ return res.status(401).json({ message: "Unauthorized" });
}
if (teamAccess.role !== "ADMIN" && teamAccess.role !== "MANAGER") {
@@ -79,9 +78,11 @@ export default async function handle(
errorhandler(error, res);
}
} else {
- // We only allow GET, PUT and DELETE requests
+ // We only allow DELETE requests
res.setHeader("Allow", ["DELETE"]);
- return res.status(405).end(`Method ${req.method} Not Allowed`);
+ return res
+ .status(405)
+ .json({ message: `Method ${req.method} Not Allowed` });
}
}