From 55308eda60348a06608dd920c03c204d6283c622 Mon Sep 17 00:00:00 2001 From: Sainan <63328889+Sainan@users.noreply.github.com> Date: Sun, 29 Jun 2025 19:45:23 +0200 Subject: [PATCH] chore: continue execution if subsequent JSON.parse on config failed By calling JSON.parse before setting everything to undefined, we don't lose the old config in case of an error. --- src/services/configService.ts | 4 +++- src/services/configWatcherService.ts | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/services/configService.ts b/src/services/configService.ts index 67fca37e..e96ace26 100644 --- a/src/services/configService.ts +++ b/src/services/configService.ts @@ -103,11 +103,13 @@ export const config: IConfig = { }; export const loadConfig = (): void => { + const newConfig = JSON.parse(fs.readFileSync(configPath, "utf-8")) as IConfig; + // Set all values to undefined now so if the new config.json omits some fields that were previously present, it's correct in-memory. for (const key of Object.keys(config)) { // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access (config as any)[key] = undefined; } - Object.assign(config, JSON.parse(fs.readFileSync(configPath, "utf-8"))); + Object.assign(config, newConfig); }; diff --git a/src/services/configWatcherService.ts b/src/services/configWatcherService.ts index cab235ff..4dea6eea 100644 --- a/src/services/configWatcherService.ts +++ b/src/services/configWatcherService.ts @@ -14,8 +14,8 @@ chokidar.watch(configPath).on("change", () => { try { loadConfig(); } catch (e) { - logger.error("FATAL ERROR: Config failed to be reloaded: " + (e as Error).message); - process.exit(1); + logger.error("Config changes were not applied: " + (e as Error).message); + return; } validateConfig(); syncConfigWithDatabase(); -- 2.47.2