-
-
Save cdaringe/d6a2ec42b6cf5f39b462 to your computer and use it in GitHub Desktop.
Node’s Uncaught Exception
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
'use strict'; | |
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) { | |
console.log('handler executed', error.message, error.stack); | |
process.exit(1); | |
} | |
process.on('uncaughtException', errorHandler); | |
process.on('unhandledRejection', errorHandler); | |
process.on('exit', function() { | |
console.log('Shutting down server…'); | |
}); | |
program | |
.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), | |
}, | |
}; | |
} | |
console.log('Starting server…'); | |
server(config) | |
.then(() => console.log('Server ready')) | |
.catch(errorHandler); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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