From d10370c7b0165092b28d5e0c89bfaa5eabcb0c4c Mon Sep 17 00:00:00 2001 From: Jake Bolam Date: Thu, 27 Dec 2018 07:19:46 +0800 Subject: [PATCH] infra: better error handling of invalid config file (#124) **What**: When a config file is loaded, but the contents cannot be parsed to JSON an error will be thrown. Instead of failing silent: e.g.: screen shot 2018-12-23 at 5 04 17 pm **Why**: Previously some commands would crash randomly. e.g. `add` with `Cannot read property 'then' of null`. While other commands e.g. `generate` would succeed but would blow away all your contributors (because it didn't find any) **How**: By Me **Checklist**: - [ ] Documentation N/A - [ ] Tests N/A - [x] Ready to be merged - [ ] Added myself to contributors table --- src/cli.js | 2 +- src/util/config-file.js | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/cli.js b/src/cli.js index b83a5b6..96f0895 100755 --- a/src/cli.js +++ b/src/cli.js @@ -40,7 +40,7 @@ const yargv = yargs try { return util.configFile.readConfig(configPath) } catch (error) { - if (configPath !== defaultRCFile) { + if (error instanceof SyntaxError || configPath !== defaultRCFile) { onError(error) } } diff --git a/src/util/config-file.js b/src/util/config-file.js index 34bd561..661b5d0 100644 --- a/src/util/config-file.js +++ b/src/util/config-file.js @@ -10,6 +10,9 @@ function readConfig(configPath) { } return config } catch (error) { + if (error instanceof SyntaxError) { + throw new SyntaxError(`Configuration file has malformed JSON: ${configPath}. Error:: ${error.message}`) + } if (error.code === 'ENOENT') { throw new Error(`Configuration file not found: ${configPath}`) } @@ -24,7 +27,7 @@ function writeConfig(configPath, content) { if (!content.projectName) { throw new Error(`Error! Project name is not set in ${configPath}`) } - + if (content.files && !content.files.length) { throw new Error( `Error! Project files was overridden and is empty in ${configPath}`,