Skip to content

Instantly share code, notes, and snippets.

@swashcap
Created February 15, 2016 22:27
Show Gist options
  • Save swashcap/9fcb28d4ecd273cab13a to your computer and use it in GitHub Desktop.
Save swashcap/9fcb28d4ecd273cab13a to your computer and use it in GitHub Desktop.
Node’s Uncaught Exception
#!/usr/bin/env node
'use strict';
const logger = require('../src/utils/logger.js');
const pkg = require('../package.json');
const program = require('commander');
const server = require('../index.js').server;
const url = require('url');
const config = {};
/**
* Error handler for server errors, process `uncaughtException`s and
* process `unhandledRejection`s.
*
* @param {Error} error
* @param {Promise} [promise]
*/
function errorHandler(error, promise) {
if (typeof promise === 'undefined') {
logger.error('Server error', error);
} else {
logger.error('Server promise rejection', error, promise);
}
process.exit(1);
}
process.on('uncaughtException', errorHandler);
process.on('unhandledRejection', errorHandler);
process.on('exit', function() {
logger.info('Shutting down server…');
});
program
.version(pkg.version)
.option('-db, --database', 'Database connection string')
.option('-s, --seed [value]', 'Seed the consortiameta database');
program.on('--help', function() {
console.log(` Databases:
Specify the CouchDB database connection as a URL string:
$ coinstac-server-core --database http://localhost:5984
Seeding:
Pass the '--seed' flag to use the built-in seed documents. You may also pass
in the path to your custom consortiameta JSON file:
$ coinstac-server-core --seed ./path/to/my/docs.json
`);
});
program.parse(process.argv);
if (typeof program.seed === 'boolean' || typeof program.seed === 'string') {
config.seed = program.seed;
}
if (typeof program.database === 'string') {
config.db = {
remote: {
db: url.parse(program.database),
},
};
}
logger.info('Starting server…');
server(config)
.then(() => logger.info('Server ready'))
.catch(errorHandler);
'use strict';
module.exports = {
server: function() {
throw new Error('imma error');
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment