From cacde7f72d4994ac85c50129d59dc8da381cb8ca Mon Sep 17 00:00:00 2001 From: Calvin Montgomery Date: Wed, 23 Aug 2017 23:02:08 -0700 Subject: [PATCH] Fix unhandled rejections in webserver --- src/web/webserver.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/web/webserver.js b/src/web/webserver.js index 37f317ff..d1c1762c 100644 --- a/src/web/webserver.js +++ b/src/web/webserver.js @@ -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);