Add exception handling to stats writer

This commit is contained in:
Cohee 2023-10-27 01:03:54 +03:00
parent 11cc27d9c9
commit 167b2d0fe4
1 changed files with 8 additions and 4 deletions

View File

@ -173,8 +173,12 @@ async function loadStatsFile(chatsPath, charactersPath, recreateStats = false) {
async function saveStatsToFile() { async function saveStatsToFile() {
if (charStats.timestamp > lastSaveTimestamp) { if (charStats.timestamp > lastSaveTimestamp) {
//console.debug("Saving stats to file..."); //console.debug("Saving stats to file...");
await writeFile(statsFilePath, JSON.stringify(charStats)); try {
lastSaveTimestamp = Date.now(); await writeFile(statsFilePath, JSON.stringify(charStats));
lastSaveTimestamp = Date.now();
} catch (error) {
console.log("Failed to save stats to file.", error);
}
} else { } else {
//console.debug('Stats have not changed since last save. Skipping file write.'); //console.debug('Stats have not changed since last save. Skipping file write.');
} }
@ -184,9 +188,9 @@ async function saveStatsToFile() {
* Attempts to save charStats to a file and then terminates the process. * Attempts to save charStats to a file and then terminates the process.
* If an error occurs during the file write, it logs the error before exiting. * If an error occurs during the file write, it logs the error before exiting.
*/ */
async function writeStatsToFileAndExit(charStats) { async function writeStatsToFileAndExit() {
try { try {
await saveStatsToFile(charStats); await saveStatsToFile();
} catch (err) { } catch (err) {
console.error("Failed to write stats to file:", err); console.error("Failed to write stats to file:", err);
} finally { } finally {