Skip to content

Commit

Permalink
Restore the config to defaults in case it gets corrupted.
Browse files Browse the repository at this point in the history
  • Loading branch information
kafeijao committed Mar 26, 2023
1 parent c4edc6d commit 1c96c37
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions server/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}


Expand Down

0 comments on commit 1c96c37

Please sign in to comment.