From 5ac2ed8fc67ad6c0ea072d1a7a278ddcf58a441c Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Fri, 19 May 2017 16:36:29 +0100 Subject: [PATCH] use a separate error handler for api router errors --- .../web/app/coffee/Features/Errors/ErrorController.coffee | 8 ++++++++ services/web/app/coffee/infrastructure/Server.coffee | 3 ++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/services/web/app/coffee/Features/Errors/ErrorController.coffee b/services/web/app/coffee/Features/Errors/ErrorController.coffee index 45a743f282..46f187e44f 100644 --- a/services/web/app/coffee/Features/Errors/ErrorController.coffee +++ b/services/web/app/coffee/Features/Errors/ErrorController.coffee @@ -28,3 +28,11 @@ module.exports = ErrorController = else logger.error err: error, url:req.url, method:req.method, user:user, "error passed to top level next middlewear" ErrorController.serverError req, res + + handleApiError: (error, req, res, next) -> + if error instanceof Errors.NotFoundError + logger.warn {err: error, url: req.url}, "not found error" + res.sendStatus(404) + else + logger.error err: error, url:req.url, method:req.method, user:user, "error passed to top level next middlewear" + res.sendStatus(500) diff --git a/services/web/app/coffee/infrastructure/Server.coffee b/services/web/app/coffee/infrastructure/Server.coffee index b9ef1b7b31..4c031a8bd1 100644 --- a/services/web/app/coffee/infrastructure/Server.coffee +++ b/services/web/app/coffee/infrastructure/Server.coffee @@ -164,11 +164,12 @@ server = require('http').createServer(app) # process api routes first, if nothing matched fall though and use # web middlewear + routes app.use(apiRouter) +app.use(ErrorController.handleApiError) app.use(webRouter) +app.use(ErrorController.handleError) router = new Router(webRouter, apiRouter) -app.use ErrorController.handleError module.exports = app: app