Browse Source

Better error handling for HTTP server

Pavel Ševčík 3 years ago
parent
commit
3088d45a1f
No known key found for this signature in database GPG Key ID: CFA54E4C0CD58DF0
  1. 6
      lib/http-server/http-server.js

6
lib/http-server/http-server.js

@ -32,13 +32,17 @@ class HttpServer {
// Initialize the tiny-http app
this.app = new App({
noMatchHandler: (req, res) => {
Logger.error(null, `HttpServer : 404 - Not found: ${req.method} - ${req.hostname}${req.path}`)
HttpServer.sendError(res, {status: '404 - Not found'}, 404)
},
// Error handler
onError: (err, req, res) => {
// Detect if this is auth error
if (Object.values(errors.auth).includes(err)) {
HttpServer.sendError(res, err, 401)
} else {
Logger.error(err.stack, 'HttpServer : general error')
Logger.error(err.stack || err, 'HttpServer : general error')
const ret = {status: 'Server error'}
HttpServer.sendError(res, ret, 500)
}

Loading…
Cancel
Save