From 0f445565fdd14cb22c0a9bb6ef632efbb89fab68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20=C5=A0ev=C4=8D=C3=ADk?= Date: Thu, 17 Jun 2021 15:30:31 +0200 Subject: [PATCH] Use onError handler for general errors --- lib/http-server/http-server.js | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/lib/http-server/http-server.js b/lib/http-server/http-server.js index 82b00aa..84a3028 100644 --- a/lib/http-server/http-server.js +++ b/lib/http-server/http-server.js @@ -30,7 +30,14 @@ class HttpServer { this.server = null // Initialize the tiny-http app - this.app = new App(); + this.app = new App({ + // Error handler + onError: (err, req, res) => { + Logger.error(err.stack, 'HttpServer : general error') + const ret = {status: 'Server error'} + HttpServer.sendError(res, ret, 500) + } + }); this.app.set('trust proxy', 'loopback') // Middlewares for json responses and requests logging @@ -50,14 +57,6 @@ class HttpServer { * @returns {object} returns the listening server instance */ start() { - // Error handler, should be final middleware - this.app.use(function(err, req, res, next) { - if (res.headersSent) return next(err) - Logger.error(err.stack, 'HttpServer : start()') - const ret = {status: 'Server error'} - HttpServer.sendError(res, ret, 500) - }) - // Start a http server this.server = this.app.listen(this.port, this.host, () => { Logger.info(`HttpServer : Listening on ${this.host}:${this.port}`)