feat: implement logging for document fetch and save operations in editor

Fetch:
1. Fetch error (HTTP) - ERROR
2. Missing BlockNote content - WARNING
3. Fetch success - INFO
4. Fetch exception - ERROR

Save:
5. New note save error - ERROR
6. New note created - INFO
7. Existing doc save error - ERROR
8. Existing doc saved - INFO
9. Save exception - ERROR
This commit is contained in:
Anish Sarkar 2025-12-17 02:04:57 +05:30
parent 4cef8cc93f
commit 8bb3c854ee

View file

@ -22,6 +22,7 @@ import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/com
import { Separator } from "@/components/ui/separator"; import { Separator } from "@/components/ui/separator";
import { notesApiService } from "@/lib/apis/notes-api.service"; import { notesApiService } from "@/lib/apis/notes-api.service";
import { authenticatedFetch, getBearerToken, redirectToLogin } from "@/lib/auth-utils"; import { authenticatedFetch, getBearerToken, redirectToLogin } from "@/lib/auth-utils";
import { useLogs } from "@/hooks/use-logs";
interface EditorContent { interface EditorContent {
document_id: number; document_id: number;
@ -68,6 +69,8 @@ export default function EditorPage() {
const searchSpaceId = Number(params.search_space_id); const searchSpaceId = Number(params.search_space_id);
const isNewNote = documentId === "new"; const isNewNote = documentId === "new";
const { createLog } = useLogs(searchSpaceId);
const [document, setDocument] = useState<EditorContent | null>(null); const [document, setDocument] = useState<EditorContent | null>(null);
const [loading, setLoading] = useState(true); const [loading, setLoading] = useState(true);
const [saving, setSaving] = useState(false); const [saving, setSaving] = useState(false);
@ -112,28 +115,101 @@ export default function EditorPage() {
const errorData = await response const errorData = await response
.json() .json()
.catch(() => ({ detail: "Failed to fetch document" })); .catch(() => ({ detail: "Failed to fetch document" }));
throw new Error(errorData.detail || "Failed to fetch document"); const errorMessage = errorData.detail || "Failed to fetch document";
// Log fetch error
try {
await createLog({
level: "ERROR",
status: "FAILED",
message: `Failed to fetch document: ${errorMessage}`,
source: "editor",
search_space_id: searchSpaceId,
log_metadata: {
document_id: documentId,
error_type: "fetch_error",
http_status: response.status,
},
});
} catch (err) {
console.error("Failed to create log:", err);
}
throw new Error(errorMessage);
} }
const data = await response.json(); const data = await response.json();
// Check if blocknote_document exists // Check if blocknote_document exists
if (!data.blocknote_document) { if (!data.blocknote_document) {
setError( const errorMsg = "This document does not have BlockNote content. Please re-upload the document to enable editing.";
"This document does not have BlockNote content. Please re-upload the document to enable editing."
); // Log missing BlockNote content
try {
await createLog({
level: "WARNING",
status: "FAILED",
message: `Document ${documentId} does not have BlockNote content`,
source: "editor",
search_space_id: searchSpaceId,
log_metadata: {
document_id: documentId,
error_type: "missing_blocknote_content",
},
});
} catch (err) {
console.error("Failed to create log:", err);
}
setError(errorMsg);
setLoading(false); setLoading(false);
return; return;
} }
// Log successful fetch
try {
await createLog({
level: "INFO",
status: "SUCCESS",
message: `Document ${documentId} loaded successfully`,
source: "editor",
search_space_id: searchSpaceId,
log_metadata: {
document_id: documentId,
document_type: data.document_type,
title: data.title,
},
});
} catch (err) {
console.error("Failed to create log:", err);
}
setDocument(data); setDocument(data);
setEditorContent(data.blocknote_document); setEditorContent(data.blocknote_document);
setError(null); setError(null);
} catch (error) { } catch (error) {
console.error("Error fetching document:", error); console.error("Error fetching document:", error);
setError( const errorMessage =
error instanceof Error ? error.message : "Failed to fetch document. Please try again." error instanceof Error ? error.message : "Failed to fetch document. Please try again.";
);
// Log general fetch error
try {
await createLog({
level: "ERROR",
status: "FAILED",
message: `Error fetching document: ${errorMessage}`,
source: "editor",
search_space_id: searchSpaceId,
log_metadata: {
document_id: documentId,
error_type: "fetch_exception",
},
});
} catch (err) {
console.error("Failed to create log:", err);
}
setError(errorMessage);
} finally { } finally {
setLoading(false); setLoading(false);
} }
@ -142,7 +218,7 @@ export default function EditorPage() {
if (documentId) { if (documentId) {
fetchDocument(); fetchDocument();
} }
}, [documentId, params.search_space_id, isNewNote]); }, [documentId, params.search_space_id, isNewNote, searchSpaceId, createLog]);
// Track changes to mark as unsaved // Track changes to mark as unsaved
useEffect(() => { useEffect(() => {
@ -204,10 +280,49 @@ export default function EditorPage() {
const errorData = await response const errorData = await response
.json() .json()
.catch(() => ({ detail: "Failed to save document" })); .catch(() => ({ detail: "Failed to save document" }));
// Log save error
try {
await createLog({
level: "ERROR",
status: "FAILED",
message: `Failed to save new note: ${errorData.detail || "Unknown error"}`,
source: "editor",
search_space_id: searchSpaceId,
log_metadata: {
document_id: note.id,
is_new_note: true,
action: "save",
http_status: response.status,
},
});
} catch (err) {
console.error("Failed to create log:", err);
}
throw new Error(errorData.detail || "Failed to save document"); throw new Error(errorData.detail || "Failed to save document");
} }
} }
// Log successful note creation
try {
await createLog({
level: "INFO",
status: "SUCCESS",
message: `Note created successfully: ${title}`,
source: "editor",
search_space_id: searchSpaceId,
log_metadata: {
document_id: note.id,
is_new_note: true,
action: "save",
title: title,
},
});
} catch (err) {
console.error("Failed to create log:", err);
}
setHasUnsavedChanges(false); setHasUnsavedChanges(false);
toast.success("Note created successfully! Reindexing in background..."); toast.success("Note created successfully! Reindexing in background...");
@ -248,9 +363,46 @@ export default function EditorPage() {
const errorData = await response const errorData = await response
.json() .json()
.catch(() => ({ detail: "Failed to save document" })); .catch(() => ({ detail: "Failed to save document" }));
// Log save error
try {
await createLog({
level: "ERROR",
status: "FAILED",
message: `Failed to save document ${documentId}: ${errorData.detail || "Unknown error"}`,
source: "editor",
search_space_id: searchSpaceId,
log_metadata: {
document_id: documentId,
action: "save",
http_status: response.status,
},
});
} catch (err) {
console.error("Failed to create log:", err);
}
throw new Error(errorData.detail || "Failed to save document"); throw new Error(errorData.detail || "Failed to save document");
} }
// Log successful save
try {
await createLog({
level: "INFO",
status: "SUCCESS",
message: `Document ${documentId} saved successfully`,
source: "editor",
search_space_id: searchSpaceId,
log_metadata: {
document_id: documentId,
action: "save",
title: document?.title,
},
});
} catch (err) {
console.error("Failed to create log:", err);
}
setHasUnsavedChanges(false); setHasUnsavedChanges(false);
toast.success("Document saved! Reindexing in background..."); toast.success("Document saved! Reindexing in background...");
@ -269,6 +421,26 @@ export default function EditorPage() {
: isNewNote : isNewNote
? "Failed to create note. Please try again." ? "Failed to create note. Please try again."
: "Failed to save document. Please try again."; : "Failed to save document. Please try again.";
// Log save error
try {
await createLog({
level: "ERROR",
status: "FAILED",
message: `Error saving document: ${errorMessage}`,
source: "editor",
search_space_id: searchSpaceId,
log_metadata: {
document_id: isNewNote ? null : documentId,
is_new_note: isNewNote,
action: "save",
error_type: "save_exception",
},
});
} catch (err) {
console.error("Failed to create log:", err);
}
setError(errorMessage); setError(errorMessage);
toast.error(errorMessage); toast.error(errorMessage);
} finally { } finally {