Browse Source

Use onError handler for general errors

umbrel
Pavel Ševčík 4 years ago
parent
commit
0f445565fd
No known key found for this signature in database GPG Key ID: D3B8A90B4072D9DB
  1. 17
      lib/http-server/http-server.js

17
lib/http-server/http-server.js

@ -30,7 +30,14 @@ class HttpServer {
this.server = null this.server = null
// Initialize the tiny-http app // 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') this.app.set('trust proxy', 'loopback')
// Middlewares for json responses and requests logging // Middlewares for json responses and requests logging
@ -50,14 +57,6 @@ class HttpServer {
* @returns {object} returns the listening server instance * @returns {object} returns the listening server instance
*/ */
start() { 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 // Start a http server
this.server = this.app.listen(this.port, this.host, () => { this.server = this.app.listen(this.port, this.host, () => {
Logger.info(`HttpServer : Listening on ${this.host}:${this.port}`) Logger.info(`HttpServer : Listening on ${this.host}:${this.port}`)

Loading…
Cancel
Save