2016-03-18 11:59:03 -04:00
|
|
|
Errors = require "./Errors"
|
|
|
|
logger = require "logger-sharelatex"
|
2016-09-22 10:33:50 -04:00
|
|
|
AuthenticationController = require '../Authentication/AuthenticationController'
|
2016-03-18 11:59:03 -04:00
|
|
|
|
2014-06-20 12:17:24 -04:00
|
|
|
module.exports = ErrorController =
|
|
|
|
notFound: (req, res)->
|
2016-03-18 11:59:03 -04:00
|
|
|
res.status(404)
|
2014-06-20 12:17:24 -04:00
|
|
|
res.render 'general/404',
|
2016-03-18 11:59:03 -04:00
|
|
|
title: "page_not_found"
|
|
|
|
|
2018-12-20 06:50:58 -05:00
|
|
|
forbidden: (req, res) ->
|
|
|
|
res.status(403)
|
|
|
|
res.render 'user/restricted'
|
|
|
|
|
2016-03-18 11:59:03 -04:00
|
|
|
serverError: (req, res)->
|
|
|
|
res.status(500)
|
|
|
|
res.render 'general/500',
|
|
|
|
title: "Server Error"
|
2016-09-22 10:33:50 -04:00
|
|
|
|
2018-10-09 06:55:14 -04:00
|
|
|
accountMergeError: (req, res)->
|
|
|
|
res.status(500)
|
|
|
|
res.render 'general/account-merge-error',
|
|
|
|
title: "Account Access Error"
|
|
|
|
|
2016-03-18 11:59:03 -04:00
|
|
|
handleError: (error, req, res, next) ->
|
2016-09-22 10:33:50 -04:00
|
|
|
user = AuthenticationController.getSessionUser(req)
|
2016-03-18 11:59:03 -04:00
|
|
|
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"
|
2016-03-18 11:59:03 -04:00
|
|
|
res.sendStatus(403)
|
|
|
|
return
|
|
|
|
if error instanceof Errors.NotFoundError
|
2016-03-21 07:55:59 -04:00
|
|
|
logger.warn {err: error, url: req.url}, "not found error"
|
2016-03-18 11:59:03 -04:00
|
|
|
ErrorController.notFound req, res
|
2018-12-20 06:50:58 -05:00
|
|
|
else if error instanceof Errors.ForbiddenError
|
|
|
|
logger.error err: error, "forbidden error"
|
|
|
|
ErrorController.forbidden req, res
|
2017-05-11 06:29:57 -04:00
|
|
|
else if error instanceof Errors.TooManyRequestsError
|
|
|
|
logger.warn {err: error, url: req.url}, "too many requests error"
|
|
|
|
res.sendStatus(429)
|
2018-10-04 05:36:10 -04:00
|
|
|
else if error instanceof Errors.InvalidError
|
|
|
|
logger.warn {err: error, url: req.url}, "invalid error"
|
|
|
|
res.status(400)
|
|
|
|
res.send(error.message)
|
2017-05-19 11:21:02 -04:00
|
|
|
else if error instanceof Errors.InvalidNameError
|
|
|
|
logger.warn {err: error, url: req.url}, "invalid name error"
|
|
|
|
res.status(400)
|
|
|
|
res.send(error.message)
|
2018-10-09 06:55:14 -04:00
|
|
|
else if error instanceof Errors.AccountMergeError
|
|
|
|
logger.error err: error, "account merge error"
|
|
|
|
ErrorController.accountMergeError req, res
|
2016-03-18 11:59:03 -04:00
|
|
|
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
|
2017-05-19 11:36:29 -04:00
|
|
|
|
|
|
|
handleApiError: (error, req, res, next) ->
|
|
|
|
if error instanceof Errors.NotFoundError
|
|
|
|
logger.warn {err: error, url: req.url}, "not found error"
|
|
|
|
res.sendStatus(404)
|
|
|
|
else
|
2017-06-26 08:56:22 -04:00
|
|
|
logger.error err: error, url:req.url, method:req.method, "error passed to top level next middlewear"
|
2017-05-19 11:36:29 -04:00
|
|
|
res.sendStatus(500)
|