Fix logging and error handling
This commit is contained in:
parent
d4bc256548
commit
c805371f0e
@ -83,8 +83,6 @@ app.use(router.allowedMethods());
|
|||||||
|
|
||||||
app.on('error', (error, ctx) => {
|
app.on('error', (error, ctx) => {
|
||||||
if (error.status) {
|
if (error.status) {
|
||||||
ctx.status = error.status;
|
|
||||||
ctx.body = error.message;
|
|
||||||
log.verbose('app', 'Request faild: %s, %s', error.status, error.message);
|
log.verbose('app', 'Request faild: %s, %s', error.status, error.message);
|
||||||
} else {
|
} else {
|
||||||
log.error('app', 'Unknown error', error, ctx);
|
log.error('app', 'Unknown error', error, ctx);
|
||||||
@ -125,11 +123,14 @@ if (!global.testing) { // don't automatically start server in tests
|
|||||||
co(function *() {
|
co(function *() {
|
||||||
let app = yield init();
|
let app = yield init();
|
||||||
app.listen(config.server.port);
|
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));
|
}).catch(err => log.error('app', 'Initialization failed!', err));
|
||||||
}
|
}
|
||||||
|
|
||||||
function *init() {
|
function *init() {
|
||||||
|
log.level = config.log.level; // set log level depending on process.env.NODE_ENV
|
||||||
injectDependencies();
|
injectDependencies();
|
||||||
|
log.verbose('app', 'Connecting to MongoDB ...');
|
||||||
yield mongo.connect();
|
yield mongo.connect();
|
||||||
return app;
|
return app;
|
||||||
}
|
}
|
||||||
|
@ -61,14 +61,14 @@ class PublicKey {
|
|||||||
// check for existing verfied key by id or email addresses
|
// check for existing verfied key by id or email addresses
|
||||||
let verified = yield this._userid.getVerfied(params);
|
let verified = yield this._userid.getVerfied(params);
|
||||||
if (verified) {
|
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
|
// delete old/unverified key and user ids with the same key id
|
||||||
yield this.remove({ keyid:params.keyid });
|
yield this.remove({ keyid:params.keyid });
|
||||||
// persist new key
|
// persist new key
|
||||||
let r = yield this._mongo.create({ _id:params.keyid, publicKeyArmored }, DB_TYPE);
|
let r = yield this._mongo.create({ _id:params.keyid, publicKeyArmored }, DB_TYPE);
|
||||||
if (r.insertedCount !== 1) {
|
if (r.insertedCount !== 1) {
|
||||||
throw util.error(500, 'Failed to persist key');
|
util.throw(500, 'Failed to persist key');
|
||||||
}
|
}
|
||||||
// persist new user ids
|
// persist new user ids
|
||||||
let userIds = yield this._userid.batch(params);
|
let userIds = yield this._userid.batch(params);
|
||||||
@ -87,7 +87,7 @@ class PublicKey {
|
|||||||
keys = this._openpgp.key.readArmored(publicKeyArmored).keys;
|
keys = this._openpgp.key.readArmored(publicKeyArmored).keys;
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
log.error('public-key', 'Failed to parse PGP key:\n%s', publicKeyArmored, 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
|
// get key user ids
|
||||||
keys.forEach(key => userIds = userIds.concat(key.getUserIds()));
|
keys.forEach(key => userIds = userIds.concat(key.getUserIds()));
|
||||||
@ -117,7 +117,7 @@ class PublicKey {
|
|||||||
userIds: email ? [{ email:email.toLowerCase() }] : undefined
|
userIds: email ? [{ email:email.toLowerCase() }] : undefined
|
||||||
});
|
});
|
||||||
if (!verified) {
|
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);
|
return yield this._mongo.get({ _id:verified.keyid }, DB_TYPE);
|
||||||
}
|
}
|
||||||
|
@ -102,8 +102,9 @@ exports.deDup = function(list) {
|
|||||||
* @param {string} message The error message
|
* @param {string} message The error message
|
||||||
* @return {Error} The resulting error object
|
* @return {Error} The resulting error object
|
||||||
*/
|
*/
|
||||||
exports.error = function(status, message) {
|
exports.throw = function(status, message) {
|
||||||
let err = new Error(message);
|
let err = new Error(message);
|
||||||
err.status = status;
|
err.status = status;
|
||||||
return err;
|
err.expose = true; // display message to the client
|
||||||
|
throw err;
|
||||||
};
|
};
|
Loading…
Reference in New Issue
Block a user