From 1c96c37fe8a4d29c51f34ea37e8923976a90755e Mon Sep 17 00:00:00 2001 From: kafeijao Date: Sun, 26 Mar 2023 18:53:45 +0100 Subject: [PATCH] Restore the config to defaults in case it gets corrupted. --- server/config.js | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/server/config.js b/server/config.js index 1bea21c..fa6f0bc 100644 --- a/server/config.js +++ b/server/config.js @@ -98,9 +98,20 @@ async function GetOrCreateJsonFile(folderPath, fileName, defaultObject = {}) { } // Get and parse the json file - const fileContents = await fs.promises.readFile(filePath, 'utf8'); - const parsedFile = JSON.parse(fileContents); - return parsedFile.data; + try { + const fileContents = await fs.promises.readFile(filePath, 'utf8'); + const parsedFile = JSON.parse(fileContents); + return parsedFile.data; + } + catch (err) { + // If we fail to read the config, let's reset the config file (needs improvement) + log.error('[GetOrCreateJsonFile] Failed to read the config file!', err.toString(), err.message?.toString()); + log.info('[GetOrCreateJsonFile] Generating a new one, using the default config...'); + await WriteToJsonFile(folderPath, fileName, defaultObject); + const fileContents = await fs.promises.readFile(filePath, 'utf8'); + const parsedFile = JSON.parse(fileContents); + return parsedFile.data; + } }