ErrorController.coffee 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. Errors = require "./Errors"
  2. logger = require "logger-sharelatex"
  3. AuthenticationController = require '../Authentication/AuthenticationController'
  4. module.exports = ErrorController =
  5. notFound: (req, res)->
  6. res.status(404)
  7. res.render 'general/404',
  8. title: "page_not_found"
  9. serverError: (req, res)->
  10. res.status(500)
  11. res.render 'general/500',
  12. title: "Server Error"
  13. handleError: (error, req, res, next) ->
  14. user = AuthenticationController.getSessionUser(req)
  15. if error?.code is 'EBADCSRFTOKEN'
  16. logger.warn err: error,url:req.url, method:req.method, user:user, "invalid csrf"
  17. res.sendStatus(403)
  18. return
  19. if error instanceof Errors.NotFoundError
  20. logger.warn {err: error, url: req.url}, "not found error"
  21. ErrorController.notFound req, res
  22. else if error instanceof Errors.TooManyRequestsError
  23. logger.warn {err: error, url: req.url}, "too many requests error"
  24. res.sendStatus(429)
  25. else if error instanceof Errors.InvalidNameError
  26. logger.warn {err: error, url: req.url}, "invalid name error"
  27. res.status(400)
  28. res.send(error.message)
  29. else
  30. logger.error err: error, url:req.url, method:req.method, user:user, "error passed to top level next middlewear"
  31. ErrorController.serverError req, res
  32. handleApiError: (error, req, res, next) ->
  33. if error instanceof Errors.NotFoundError
  34. logger.warn {err: error, url: req.url}, "not found error"
  35. res.sendStatus(404)
  36. else
  37. logger.error err: error, url:req.url, method:req.method, "error passed to top level next middlewear"
  38. res.sendStatus(500)