Fix logging and error handling

This commit is contained in:
Tankred Hase 2016-05-28 23:31:25 +02:00
parent d4bc256548
commit c805371f0e
3 changed files with 10 additions and 8 deletions

View File

@ -83,8 +83,6 @@ app.use(router.allowedMethods());
app.on('error', (error, ctx) => {
if (error.status) {
ctx.status = error.status;
ctx.body = error.message;
log.verbose('app', 'Request faild: %s, %s', error.status, error.message);
} else {
log.error('app', 'Unknown error', error, ctx);
@ -125,11 +123,14 @@ if (!global.testing) { // don't automatically start server in tests
co(function *() {
let app = yield init();
app.listen(config.server.port);
log.verbose('app', 'Ready to rock! Listening on http://localhost:' + config.server.port);
}).catch(err => log.error('app', 'Initialization failed!', err));
}
function *init() {
log.level = config.log.level; // set log level depending on process.env.NODE_ENV
injectDependencies();
log.verbose('app', 'Connecting to MongoDB ...');
yield mongo.connect();
return app;
}

View File

@ -61,14 +61,14 @@ class PublicKey {
// check for existing verfied key by id or email addresses
let verified = yield this._userid.getVerfied(params);
if (verified) {
throw util.error(304, 'Key for this user already exists: ' + verified.stringify());
util.throw(304, 'Key for this user already exists: ' + verified.stringify());
}
// delete old/unverified key and user ids with the same key id
yield this.remove({ keyid:params.keyid });
// persist new key
let r = yield this._mongo.create({ _id:params.keyid, publicKeyArmored }, DB_TYPE);
if (r.insertedCount !== 1) {
throw util.error(500, 'Failed to persist key');
util.throw(500, 'Failed to persist key');
}
// persist new user ids
let userIds = yield this._userid.batch(params);
@ -87,7 +87,7 @@ class PublicKey {
keys = this._openpgp.key.readArmored(publicKeyArmored).keys;
} catch(e) {
log.error('public-key', 'Failed to parse PGP key:\n%s', publicKeyArmored, e);
throw util.error(500, 'Failed to parse PGP key');
util.throw(500, 'Failed to parse PGP key');
}
// get key user ids
keys.forEach(key => userIds = userIds.concat(key.getUserIds()));
@ -117,7 +117,7 @@ class PublicKey {
userIds: email ? [{ email:email.toLowerCase() }] : undefined
});
if (!verified) {
throw util.error(404, 'Key not found');
util.throw(404, 'Key not found');
}
return yield this._mongo.get({ _id:verified.keyid }, DB_TYPE);
}

View File

@ -102,8 +102,9 @@ exports.deDup = function(list) {
* @param {string} message The error message
* @return {Error} The resulting error object
*/
exports.error = function(status, message) {
exports.throw = function(status, message) {
let err = new Error(message);
err.status = status;
return err;
err.expose = true; // display message to the client
throw err;
};