Fix unhandled rejections in webserver

This commit is contained in:
Calvin Montgomery 2017-08-23 23:02:08 -07:00
parent 712a8c228b
commit cacde7f72d
1 changed files with 23 additions and 0 deletions

View File

@ -121,6 +121,28 @@ function initializeErrorHandlers(app) {
});
}
function patchExpressToHandleAsync() {
const Layer = require('express/lib/router/layer');
// https://github.com/expressjs/express/blob/4.x/lib/router/layer.js#L86
Layer.prototype.handle_request = function handle(req, res, next) {
const fn = this.handle;
if (fn.length > 3) {
next();
}
try {
const result = fn(req, res, next);
if (result && result.catch) {
result.catch(error => next(error));
}
} catch (error) {
next(error);
}
};
}
module.exports = {
/**
* Initializes webserver callbacks
@ -134,6 +156,7 @@ module.exports = {
session,
globalMessageBus
) {
patchExpressToHandleAsync();
const chanPath = Config.get('channel-path');
initPrometheus(app);