chore: continue execution if subsequent JSON.parse on config failed (#2353)
By calling JSON.parse before setting everything to undefined, we don't lose the old config in case of an error. Reviewed-on: OpenWF/SpaceNinjaServer#2353 Co-authored-by: Sainan <63328889+Sainan@users.noreply.github.com> Co-committed-by: Sainan <63328889+Sainan@users.noreply.github.com>
This commit is contained in:
		
							parent
							
								
									8c19aec340
								
							
						
					
					
						commit
						1a2d8ab19a
					
				@ -103,11 +103,13 @@ export const config: IConfig = {
 | 
				
			|||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export const loadConfig = (): void => {
 | 
					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.
 | 
					    // 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)) {
 | 
					    for (const key of Object.keys(config)) {
 | 
				
			||||||
        // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
 | 
					        // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
 | 
				
			||||||
        (config as any)[key] = undefined;
 | 
					        (config as any)[key] = undefined;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    Object.assign(config, JSON.parse(fs.readFileSync(configPath, "utf-8")));
 | 
					    Object.assign(config, newConfig);
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
				
			|||||||
@ -14,8 +14,8 @@ chokidar.watch(configPath).on("change", () => {
 | 
				
			|||||||
        try {
 | 
					        try {
 | 
				
			||||||
            loadConfig();
 | 
					            loadConfig();
 | 
				
			||||||
        } catch (e) {
 | 
					        } catch (e) {
 | 
				
			||||||
            logger.error("FATAL ERROR: Config failed to be reloaded: " + (e as Error).message);
 | 
					            logger.error("Config changes were not applied: " + (e as Error).message);
 | 
				
			||||||
            process.exit(1);
 | 
					            return;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        validateConfig();
 | 
					        validateConfig();
 | 
				
			||||||
        syncConfigWithDatabase();
 | 
					        syncConfigWithDatabase();
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user