From 9e5660124841c6eaa060f8efd9bba2b03eb87dec Mon Sep 17 00:00:00 2001 From: "mtarrade.sap@gmail.com" Date: Thu, 11 Jul 2024 08:49:04 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9Bfix=20configmanager=20crashing=20wh?= =?UTF-8?q?en=20json=20is=20not=20valid?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- configmanager/server.js | 52 +++++++++++++++++++++++++++++++++-------- 1 file changed, 42 insertions(+), 10 deletions(-) diff --git a/configmanager/server.js b/configmanager/server.js index 56aab10..fd838d1 100644 --- a/configmanager/server.js +++ b/configmanager/server.js @@ -36,7 +36,13 @@ app.get('/:namespace/:application', (req, res) => { return res.json([]); } if(!decoys) return res.json([]) - const decoysJson = JSON.parse(decoys); + var decoysJson; + try { + decoysJson = JSON.parse(decoys); + } catch(e){ + console.error("File cad-default.json is not a valid json"); + return res.json([]); + } // Check if the file exists fs.access(configFilePath, fs.constants.F_OK, err => { if(err) { @@ -45,8 +51,13 @@ app.get('/:namespace/:application', (req, res) => { fs.readFile(defaultConfigFilePath, 'utf8', (err, config) => { if(err) return res.json({ decoy: decoysJson }); if (config) { - const configJson = JSON.parse(config); - return res.json({ decoy: decoysJson, config: configJson }); + try { + const configJson = JSON.parse(config); + return res.json({ decoy: decoysJson, config: configJson }); + } catch(e){ + console.error("File config-default.json is not a valid json"); + return res.json([]); + } } return res.json({ decoy: decoysJson }) }) @@ -55,8 +66,13 @@ app.get('/:namespace/:application', (req, res) => { fs.readFile(configFilePath, 'utf8', (err, config) => { if(err) return res.json({ decoy: decoysJson }); if (config) { - const configJson = JSON.parse(config); - return res.json({ decoy: decoysJson, config: configJson }); + try{ + const configJson = JSON.parse(config); + return res.json({ decoy: decoysJson, config: configJson }); + } catch(e){ + console.error(`File config-${namespace}-${application}.json is not a valid json`); + return res.json([]); + } } return res.json({ decoy: decoysJson }) }) @@ -73,7 +89,13 @@ app.get('/:namespace/:application', (req, res) => { return res.json([]); } if(!decoys) return res.json([]) - const decoysJson = JSON.parse(decoys); + var decoysJson; + try{ + decoysJson = JSON.parse(decoys); + } catch(e){ + console.error(`File cad-${namespace}-${application}.json is not a valid json`); + return res.json([]); + } // Check if the file exists fs.access(configFilePath, fs.constants.F_OK, err => { if(err) { @@ -82,8 +104,13 @@ app.get('/:namespace/:application', (req, res) => { fs.readFile(defaultConfigFilePath, 'utf8', (err, config) => { if(err) return res.json({ decoy: decoysJson }); if (config) { - const configJson = JSON.parse(config); - return res.json({ decoy: decoysJson, config: configJson }); + try{ + const configJson = JSON.parse(config); + return res.json({ decoy: decoysJson, config: configJson }); + } catch(e){ + console.log("File config-default.json is not a valid json"); + return res.json([]); + } } return res.json({ decoy: decoysJson }) }) @@ -92,8 +119,13 @@ app.get('/:namespace/:application', (req, res) => { fs.readFile(configFilePath, 'utf8', (err, config) => { if(err) return res.json({ decoy: decoysJson }); if (config) { - const configJson = JSON.parse(config); - return res.json({ decoy: decoysJson, config: configJson }); + try{ + const configJson = JSON.parse(config); + return res.json({ decoy: decoysJson, config: configJson }); + } catch(e){ + console.error(`File config-${namespace}-${application}.json is not a valid json`); + return res.json([]); + } } return res.json({ decoy: decoysJson }) })