Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 57 additions & 55 deletions components/documents/add-document-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -363,71 +363,73 @@ export function AddDocumentModal({
},
);

if (response) {
const document = await response.json();
if (!response.ok) {
const { error } = await response.json();
toast.error(error);
return;
}

if (isDataroom && dataroomId) {
const dataroomResponse = await addDocumentToDataroom({
documentId: document.id,
folderPathName: currentFolderPath?.join("/"),
});
const document = await response.json();

if (dataroomResponse?.ok) {
const dataroomDocument =
(await dataroomResponse.json()) as DataroomDocument & {
dataroom: { _count: { viewerGroups: number } };
};
if (isDataroom && dataroomId) {
const dataroomResponse = await addDocumentToDataroom({
documentId: document.id,
folderPathName: currentFolderPath?.join("/"),
});

if (dataroomDocument.dataroom._count.viewerGroups > 0) {
setShowGroupPermissions(true);
setUploadedFiles([
{
documentId: document.id,
dataroomDocumentId: dataroomDocument.id,
fileName: document.name,
},
]);
}
if (dataroomResponse?.ok) {
const dataroomDocument =
(await dataroomResponse.json()) as DataroomDocument & {
dataroom: { _count: { viewerGroups: number } };
};

if (dataroomDocument.dataroom._count.viewerGroups > 0) {
setShowGroupPermissions(true);
setUploadedFiles([
{
documentId: document.id,
dataroomDocumentId: dataroomDocument.id,
fileName: document.name,
},
]);
}
}

analytics.capture("Document Added", {
documentId: document.id,
name: document.name,
numPages: document.numPages,
path: router.asPath,
type: "notion",
analytics.capture("Document Added", {
documentId: document.id,
name: document.name,
numPages: document.numPages,
path: router.asPath,
type: "notion",
teamId: teamId,
dataroomId: dataroomId,
$set: {
teamId: teamId,
dataroomId: dataroomId,
$set: {
teamId: teamId,
teamPlan: plan,
},
});

return;
}
teamPlan: plan,
},
});

if (!newVersion) {
toast.success(
"Notion Page processed. Redirecting to document page...",
);
return;
}

analytics.capture("Document Added", {
documentId: document.id,
name: document.name,
fileSize: null,
path: router.asPath,
type: "notion",
if (!newVersion) {
toast.success("Notion Page processed. Redirecting to document page...");

analytics.capture("Document Added", {
documentId: document.id,
name: document.name,
fileSize: null,
path: router.asPath,
type: "notion",
teamId: teamId,
$set: {
teamId: teamId,
$set: {
teamId: teamId,
teamPlan: plan,
},
});
teamPlan: plan,
},
});

// redirect to the document page
router.push("/documents/" + document.id);
}
// redirect to the document page
router.push("/documents/" + document.id);
}
} catch (error) {
setUploading(false);
Expand Down
3 changes: 2 additions & 1 deletion lib/documents/create-document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ export const createDocument = async ({
);

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
const error = await response.json();
throw new Error(error);
}
Comment on lines 51 to 54
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improved error handling, but consider refining further.

The change enhances error handling by extracting error details from the response JSON, but there are a couple of concerns:

  1. Passing an object directly to the Error constructor might result in "[object Object]" as the error message.
  2. No error handling if the response body isn't valid JSON.
if (!response.ok) {
  const error = await response.json();
-  throw new Error(error);
+  throw new Error(typeof error === 'string' ? error : error.message || JSON.stringify(error));
}
🤖 Prompt for AI Agents
In lib/documents/create-document.ts around lines 51 to 54, the error handling
currently throws a new Error with the entire error object, which results in an
unhelpful "[object Object]" message and lacks handling for invalid JSON
responses. To fix this, parse the response JSON inside a try-catch block,
extract a meaningful error message string from the parsed object, and throw a
new Error with that message. If parsing fails, throw a generic error indicating
the response could not be parsed.


return response;
Expand Down