Skip to content
This repository has been archived by the owner on Sep 2, 2023. It is now read-only.

Commit

Permalink
[TASK] Provide better error message for missing config file
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Clark committed Feb 12, 2015
1 parent 3fd917c commit b34f5d4
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 55 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
node_modules/
accounts.json
*.sqlite
config.json
/config.json
.DS_Store
coverage/
.idea/
*.db
*.db
41 changes: 30 additions & 11 deletions api/lib/config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
var fs = require('fs');
var path = require('path');
var nconf = require('nconf');
var JaySchema = require('jayschema');
var formatJaySchemaErrors = require('jayschema-error-messages');
var configSchema = require('../../schemas/config.json');
var exampleConfig = require('../../config-example.json');

/**
* Resolve absolute path of configuration property
*/

function resolvePath(p) {
return path.resolve(__dirname, '..', p);
return path.resolve(__dirname, '../..', p);
};

/**
Expand All @@ -28,14 +33,24 @@ var configPath = nconf.get('config')
|| process.env['TEST_CONFIG']
|| path.join(process.cwd(), 'config.json');

// Load config.json
if (nconf.get('NODE_ENV') !== 'test' && !fs.existsSync(configPath)) {
console.error('ERROR: configuration file not found or not accessible at: '
+ configPath);
process.exit(1);
}

// Load config.json, nconf.file does not fail if file does not exist
try {
nconf.file(configPath);
} catch (e) {
if (nconf.get('checkconfig')) {
console.error(e);
process.exit(1);
}
console.error(e);
process.exit(1);
}

if (nconf.get('NODE_ENV') === 'test') {
nconf.set('port', exampleConfig.port);
nconf.set('host', exampleConfig.host);
nconf.set('rippled_servers', exampleConfig.rippled_servers);
}

// Override `rippled_servers` with `rippled` if it exists
Expand All @@ -45,6 +60,15 @@ if (/^(wss|ws):\/\/.+:[0-9]+$/.test(nconf.get('rippled'))) {
});
}

// check that config matches the required schema
var schemaValidator = new JaySchema();
var schemaErrors = schemaValidator.validate(nconf.get(), configSchema);
if (schemaErrors.length > 0) {
console.error('ERROR: Invalid configuration');
console.error(JSON.stringify(formatJaySchemaErrors(schemaErrors), null, 2));
process.exit(1);
}

if (nconf.get('db_path')) {
// Resolve absolute db_path
if (nconf.get('db_path') !== ':memory:') {
Expand All @@ -71,11 +95,6 @@ if (nconf.get('ssl')) {
nconf.set('ssl', sslConfig);
}

if (nconf.get('NODE_ENV') === 'test') {
nconf.set('port', require('../../config-example').port);
nconf.set('host', require('../../config-example').host);
}

// Print configuration and exit
if (nconf.get('checkconfig')) {
console.log(JSON.stringify(nconf.get(), null, 2));
Expand Down
71 changes: 39 additions & 32 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 11 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,34 @@
"node": "~0.10.22"
},
"dependencies": {
"async": "~0.2.9",
"bignumber.js": "~1.4.0",
"async": "^0.2.9",
"bignumber.js": "^1.4.0",
"bluebird": "^2.3.4",
"body-parser": "^1.7.0",
"compression": "^1.3.0",
"express": "^4.8.7",
"jayschema": "*",
"knex": "0.7.3",
"lodash": "~2.4.1",
"jayschema": "^0.3.1",
"jayschema-error-messages": "^1.0.2",
"knex": "^0.7.3",
"lodash": "^2.4.1",
"morgan": "^1.3.0",
"nconf": "~0.6.9",
"node-uuid": "~1.4.1",
"nconf": "^0.6.9",
"node-uuid": "^1.4.1",
"ripple-lib": "^0.12.0",
"ripple-lib-transactionparser": "^0.3.0",
"sqlite3": "~3.0.2",
"sqlite3": "^3.0.2",
"supertest": "^0.13.0",
"winston": "^0.7.3"
},
"devDependencies": {
"assert-diff": "^0.0.4",
"coveralls": "~2.10.0",
"chai": "^1.10.0",
"coveralls": "^2.10.0",
"istanbul": "^0.2.10",
"mocha": "^2.1.0",
"require-directory": "^1.2.0",
"sinon": "~1.10.0",
"sinon-chai": "~2.5.0",
"sinon-chai": "^2.5.0",
"ws": "^0.4.32"
}
}
17 changes: 17 additions & 0 deletions schemas/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "config.json",
"description": "The Ripple-REST configuration file",
"type": "object",
"properties": {
"rippled_servers": {
"type": "array",
"items": {
"type": "string"
},
"minItems": 1,
"uniqueItems": true
}
},
"required": ["rippled_servers"]
}

0 comments on commit b34f5d4

Please sign in to comment.