From 167b2d0fe468c8eac04540d04f47d976aad683b9 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Fri, 27 Oct 2023 01:03:54 +0300 Subject: [PATCH] Add exception handling to stats writer --- statsHelpers.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/statsHelpers.js b/statsHelpers.js index ee344d303..c370fa588 100644 --- a/statsHelpers.js +++ b/statsHelpers.js @@ -173,8 +173,12 @@ async function loadStatsFile(chatsPath, charactersPath, recreateStats = false) { async function saveStatsToFile() { if (charStats.timestamp > lastSaveTimestamp) { //console.debug("Saving stats to file..."); - await writeFile(statsFilePath, JSON.stringify(charStats)); - lastSaveTimestamp = Date.now(); + try { + await writeFile(statsFilePath, JSON.stringify(charStats)); + lastSaveTimestamp = Date.now(); + } catch (error) { + console.log("Failed to save stats to file.", error); + } } else { //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. * If an error occurs during the file write, it logs the error before exiting. */ -async function writeStatsToFileAndExit(charStats) { +async function writeStatsToFileAndExit() { try { - await saveStatsToFile(charStats); + await saveStatsToFile(); } catch (err) { console.error("Failed to write stats to file:", err); } finally {