Skip to content

Commit

Permalink
fix(config): better errors if file invalid or does not exist
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
vojtajina committed Jun 28, 2013
1 parent d2a3c85 commit 74b533b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
14 changes: 10 additions & 4 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 1 addition & 11 deletions test/unit/config.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down

0 comments on commit 74b533b

Please sign in to comment.