From 74b533beb34c115f5080d412a03573d269d540aa Mon Sep 17 00:00:00 2001 From: Vojta Jina Date: Thu, 27 Jun 2013 18:51:22 -0700 Subject: [PATCH] fix(config): better errors if file invalid or does not exist Remove one test, that does not make any sense now that we require() config files - you can specify config file as a directory, assuming it's a valid node module (contains index.js or package.json). --- lib/config.js | 14 ++++++++++---- test/unit/config.spec.coffee | 12 +----------- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/lib/config.js b/lib/config.js index 252a79b5c..3baffd3f2 100644 --- a/lib/config.js +++ b/lib/config.js @@ -276,21 +276,27 @@ var Config = function() { }; }; +var CONFIG_SYNTAX_HELP = ' module.exports = function(config) {\n' + + ' config.set({\n' + + ' // your config\n' + + ' });\n' + + ' };\n'; + var parseConfig = function(configFilePath, cliOptions) { var configModule; if (configFilePath) { try { configModule = require(configFilePath); } catch(e) { - if (e.code === 'MODULE_NOT_FOUND') { - log.error('Config file does not exist!'); + if (e.code === 'MODULE_NOT_FOUND' && e.message.indexOf(configFilePath) !== -1) { + log.error('File %s does not exist!', configFilePath); } else { - log.error('Invalid config file!\n', e); + log.error('Invalid config file!\n ' + e.stack); } return process.exit(1); } if (!helper.isFunction(configModule)) { - log.error('Config file must export a function!'); + log.error('Config file must export a function!\n' + CONFIG_SYNTAX_HELP); return process.exit(1); } } else { diff --git a/test/unit/config.spec.coffee b/test/unit/config.spec.coffee index 67c4a6efe..e8eed6257 100644 --- a/test/unit/config.spec.coffee +++ b/test/unit/config.spec.coffee @@ -112,17 +112,7 @@ describe 'config', -> expect(logSpy).to.have.been.called event = logSpy.lastCall.args[0] expect(event.level.toString()).to.be.equal 'ERROR' - expect(event.data).to.be.deep.equal ['Config file does not exist!'] - expect(mocks.process.exit).to.have.been.calledWith 1 - - - it 'should log error and exit if it is a directory', -> - e.parseConfig '/conf', {} - - expect(logSpy).to.have.been.called - event = logSpy.lastCall.args[0] - expect(event.level.toString()).to.be.equal 'ERROR' - expect(event.data).to.be.deep.equal ['Config file does not exist!'] + expect(event.data).to.be.deep.equal ['File %s does not exist!', '/conf/not-exist.js'] expect(mocks.process.exit).to.have.been.calledWith 1