From ab57edef331ca3d88e48f60070f254a77d8074b6 Mon Sep 17 00:00:00 2001 From: muxator Date: Sun, 10 Mar 2019 00:26:36 +0100 Subject: [PATCH] Settings.js: exit gracefully if an invalid credentials.json is passed. Before this commit, when passed a malformed credentials.json the application crashed with a stack dump. Now we catch the error and fail in a controlled way (like already done for settings.json). Example of exception we no longer throw: MALFORMEDJSON ^ SyntaxError: Unexpected token M in JSON at position 0 at JSON.parse () at Object.reloadSettings (/src/node/utils/Settings.js:390:24) at Object. (/src/node/utils/Settings.js:543:9) at Module._compile (module.js:635:30) at Object.Module._extensions..js (module.js:646:10) at Module.load (module.js:554:32) at tryModuleLoad (module.js:497:12) at Function.Module._load (module.js:489:3) at Module.require (module.js:579:17) at require (internal/module.js:11:18) --- src/node/utils/Settings.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/node/utils/Settings.js b/src/node/utils/Settings.js index 206413c84..98756c27f 100644 --- a/src/node/utils/Settings.js +++ b/src/node/utils/Settings.js @@ -379,9 +379,14 @@ exports.reloadSettings = function reloadSettings() { process.exit(1); } - if (credentialsStr) { - credentialsStr = jsonminify(credentialsStr).replace(",]","]").replace(",}","}"); - credentials = JSON.parse(credentialsStr); + try { + if (credentialsStr) { + credentialsStr = jsonminify(credentialsStr).replace(",]","]").replace(",}","}"); + credentials = JSON.parse(credentialsStr); + } + } catch(e) { + console.error(`There was an error processing your credentials file from ${credentialsFilename}:` + e.message); + process.exit(1); } //loop trough the settings