Skip to content

Commit 6889ff9

Browse files
committed
Add enhanced debugging for production page save errors - detailed Firebase operation logging to identify exact failure point
1 parent fbbb904 commit 6889ff9

File tree

1 file changed

+59
-29
lines changed

1 file changed

+59
-29
lines changed

app/firebase/database/versions.ts

Lines changed: 59 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -431,9 +431,25 @@ const pageDoc = await getDoc(doc(db, getCollectionName("pages"), pageId));
431431
}
432432

433433
// Create the new version document
434-
console.log('🔵 VERSION: Creating new version document');
435-
const versionRef = await addDoc(collection(db, getCollectionName("pages"), pageId, "versions"), versionData);
436-
console.log("✅ VERSION: Created new version with ID:", versionRef.id);
434+
console.log('🔵 VERSION: Creating new version document', {
435+
pageId,
436+
collectionPath: `${getCollectionName("pages")}/${pageId}/versions`,
437+
versionDataKeys: Object.keys(versionData)
438+
});
439+
440+
let versionRef;
441+
try {
442+
versionRef = await addDoc(collection(db, getCollectionName("pages"), pageId, "versions"), versionData);
443+
console.log("✅ VERSION: Created new version with ID:", versionRef.id);
444+
} catch (versionError) {
445+
console.error("🔴 VERSION: Failed to create version document:", {
446+
error: versionError.message,
447+
code: versionError.code,
448+
pageId,
449+
collectionPath: `${getCollectionName("pages")}/${pageId}/versions`
450+
});
451+
throw versionError;
452+
}
437453

438454
// Update the page document with the new current version and content
439455
const pageUpdateData = {
@@ -453,10 +469,22 @@ const versionRef = await addDoc(collection(db, getCollectionName("pages"), pageI
453469
currentVersion: versionRef.id,
454470
contentLength: contentString.length,
455471
lastModified: now,
456-
hasDiff: !!diffResult
472+
hasDiff: !!diffResult,
473+
collectionPath: `${getCollectionName("pages")}/${pageId}`
457474
});
458-
await setDoc(doc(db, getCollectionName("pages"), pageId), pageUpdateData, { merge: true });
459-
console.log("✅ VERSION: Page document updated successfully");
475+
476+
try {
477+
await setDoc(doc(db, getCollectionName("pages"), pageId), pageUpdateData, { merge: true });
478+
console.log("✅ VERSION: Page document updated successfully");
479+
} catch (pageUpdateError) {
480+
console.error("🔴 VERSION: Failed to update page document:", {
481+
error: pageUpdateError.message,
482+
code: pageUpdateError.code,
483+
pageId,
484+
collectionPath: `${getCollectionName("pages")}/${pageId}`
485+
});
486+
throw pageUpdateError;
487+
}
460488

461489
// Note: User activity for streak tracking is handled on the client side
462490

@@ -469,38 +497,40 @@ await setDoc(doc(db, getCollectionName("pages"), pageId), pageUpdateData, { merg
469497
};
470498

471499
} catch (error) {
472-
// Enhanced error logging with comprehensive details
473-
const errorDetails = {
500+
// Enhanced error logging for production debugging
501+
const errorContext = {
502+
pageId,
503+
userId: data.userId,
504+
username: data.username,
505+
hasContent: !!data.content,
506+
contentType: typeof data.content,
507+
contentLength: data.content ? JSON.stringify(data.content).length : 0,
508+
groupId: data.groupId,
509+
environment: process.env.NODE_ENV,
510+
vercelEnv: process.env.VERCEL_ENV,
511+
collectionName: getCollectionName("pages"),
512+
timestamp: new Date().toISOString()
513+
};
514+
515+
console.error("🔴 VERSION: Error saving new version:", {
474516
error: {
475517
message: error.message,
476518
stack: error.stack,
477519
name: error.name,
478-
code: error.code,
479-
cause: error.cause
520+
code: error.code
480521
},
481-
context: {
482-
pageId,
483-
userId: data.userId,
484-
username: data.username,
485-
hasContent: !!data.content,
486-
contentType: typeof data.content,
487-
contentLength: data.content ? JSON.stringify(data.content).length : 0,
488-
groupId: data.groupId,
489-
timestamp: new Date().toISOString(),
490-
environment: process.env.NODE_ENV,
491-
buildId: process.env.VERCEL_GIT_COMMIT_SHA
492-
}
493-
};
494-
495-
console.error("🔴 VERSION: Error saving new version with comprehensive details:", errorDetails);
496-
logger.critical("Version save failed with comprehensive details", errorDetails, 'VERSION_SAVE');
522+
context: errorContext
523+
});
497524

498-
// REMOVED: Heavy error logging to prevent performance issues
525+
logger.critical("Version save failed", {
526+
error: error.message,
527+
stack: error.stack,
528+
context: errorContext
529+
}, 'VERSION_SAVE');
499530

500531
return {
501532
success: false,
502-
error: error.message,
503-
errorDetails: errorDetails // Include detailed error info for debugging
533+
error: error.message
504534
};
505535
}
506536
};

0 commit comments

Comments
 (0)