Skip to content

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

fix issue causing error on team change

Ashish Viradiya0f946e4766df
diff --git a/lib/swr/use-dataroom.ts b/lib/swr/use-dataroom.ts
index 6c52d988..a7e6d343 100644
--- a/lib/swr/use-dataroom.ts
+++ b/lib/swr/use-dataroom.ts
@@ -1,6 +1,7 @@
import { useRouter } from "next/router";
import { useMemo } from "react";
+import { toast } from "sonner";
import { useTeam } from "@/context/team-context";
import { Dataroom, DataroomDocument, DataroomFolder } from "@prisma/client";
@@ -29,7 +30,17 @@ export function useDataroom() {
const { data: dataroom, error } = useSWR<Dataroom>(
teamId && id && `/api/teams/${teamId}/datarooms/${id}`,
fetcher,
- { dedupingInterval: 10000 },
+ {
+ dedupingInterval: 10000,
+ onError: (err) => {
+ if (err.status === 404) {
+ toast.error("Dataroom not found", {
+ description: "The dataroom you're looking for doesn't exist or has been moved.",
+ });
+ router.replace("/datarooms");
+ }
+ }
+ },
);
return {
diff --git a/lib/swr/use-document.ts b/lib/swr/use-document.ts
index 82a180c7..170d5877 100644
--- a/lib/swr/use-document.ts
+++ b/lib/swr/use-document.ts
@@ -3,10 +3,9 @@ import { useRouter } from "next/router";
import { useTeam } from "@/context/team-context";
import { View } from "@prisma/client";
import useSWR from "swr";
-import useSWRImmutable from "swr/immutable";
-
import { DocumentWithVersion, LinkWithViews } from "@/lib/types";
import { fetcher } from "@/lib/utils";
+import { toast } from "sonner";
export function useDocument() {
const router = useRouter();
@@ -29,6 +28,14 @@ export function useDocument() {
fetcher,
{
dedupingInterval: 10000,
+ onError: (err) => {
+ if (err.status === 404) {
+ toast.error("Document not found", {
+ description: "The document you're looking for doesn't exist or has been moved.",
+ });
+ router.replace("/documents");
+ }
+ }
},
);
diff --git a/pages/api/teams/[teamId]/datarooms/[id]/index.ts b/pages/api/teams/[teamId]/datarooms/[id]/index.ts
index 66167216..7b0ff596 100644
--- a/pages/api/teams/[teamId]/datarooms/[id]/index.ts
+++ b/pages/api/teams/[teamId]/datarooms/[id]/index.ts
@@ -49,6 +49,13 @@ export default async function handle(
},
});
+ if (!dataroom) {
+ return res.status(404).json({
+ error: "Not Found",
+ message: "The requested dataroom does not exist",
+ });
+ }
+
return res.status(200).json(dataroom);
} catch (error) {
errorhandler(error, res);
diff --git a/pages/api/teams/[teamId]/documents/[id]/index.ts b/pages/api/teams/[teamId]/documents/[id]/index.ts
index c926b0e4..c51add6d 100644
--- a/pages/api/teams/[teamId]/documents/[id]/index.ts
+++ b/pages/api/teams/[teamId]/documents/[id]/index.ts
@@ -2,7 +2,7 @@ import { NextApiRequest, NextApiResponse } from "next";
import { getServerSession } from "next-auth/next";
-import { errorhandler } from "@/lib/errorHandler";
+import { errorhandler, TeamError } from "@/lib/errorHandler";
import { deleteFile } from "@/lib/files/delete-file-server";
import prisma from "@/lib/prisma";
import { getTeamWithUsersAndDocument } from "@/lib/team/helper";
@@ -59,7 +59,10 @@ export default async function handle(
});
if (!document || !document.versions || document.versions.length === 0) {
- return res.status(404).end("Document not found");
+ return res.status(404).json({
+ error: "Not Found",
+ message: "The requested document does not exist",
+ });
}
const pages = await prisma.documentPage.findMany({
@@ -85,6 +88,12 @@ export default async function handle(
return res.status(200).json({ ...document, hasPageLinks });
} catch (error) {
+ if (error instanceof TeamError) {
+ return res.status(404).json({
+ error: "Not Found",
+ message: error.message,
+ });
+ }
errorhandler(error, res);
}
} else if (req.method === "PUT") {
diff --git a/pages/documents/[id]/index.tsx b/pages/documents/[id]/index.tsx
index a5cb22ff..a9744d79 100644
--- a/pages/documents/[id]/index.tsx
+++ b/pages/documents/[id]/index.tsx
@@ -34,10 +34,6 @@ export default function DocumentPage() {
const [isLinkSheetOpen, setIsLinkSheetOpen] = useState<boolean>(false);
- if (error && error.status === 404) {
- return <ErrorPage statusCode={404} />;
- }
-
if (error && error.status === 400) {
return <ErrorPage statusCode={400} />;
}