overleaf/services/web/app/coffee/Features/Errors/ErrorController.coffee

43 lines
1.5 KiB
CoffeeScript
Raw Normal View History

Errors = require "./Errors"
logger = require "logger-sharelatex"
2016-09-22 10:33:50 -04:00
AuthenticationController = require '../Authentication/AuthenticationController'
2014-06-20 12:17:24 -04:00
module.exports = ErrorController =
notFound: (req, res)->
res.status(404)
2014-06-20 12:17:24 -04:00
res.render 'general/404',
title: "page_not_found"
serverError: (req, res)->
res.status(500)
res.render 'general/500',
title: "Server Error"
2016-09-22 10:33:50 -04:00
handleError: (error, req, res, next) ->
2016-09-22 10:33:50 -04:00
user = AuthenticationController.getSessionUser(req)
if error?.code is 'EBADCSRFTOKEN'
2016-09-22 10:33:50 -04:00
logger.warn err: error,url:req.url, method:req.method, user:user, "invalid csrf"
res.sendStatus(403)
return
if error instanceof Errors.NotFoundError
logger.warn {err: error, url: req.url}, "not found error"
ErrorController.notFound req, res
else if error instanceof Errors.TooManyRequestsError
logger.warn {err: error, url: req.url}, "too many requests error"
res.sendStatus(429)
else if error instanceof Errors.InvalidNameError
logger.warn {err: error, url: req.url}, "invalid name error"
res.status(400)
res.send(error.message)
else
2016-09-22 10:33:50 -04:00
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, "error passed to top level next middlewear"
res.sendStatus(500)