Merge pull request #2985 from overleaf/msm-oerror-remove-unprocessable-entity-error

Replace UnprocessableEntityError with calls to unprocessableEntity() handler

GitOrigin-RevId: 4bba389c8cdf87a40137d49db571fa81aaac4239
This commit is contained in:
Miguel Serrano
2020-07-16 08:47:46 +02:00
committed by Copybot
parent 397bd034c7
commit 6562f3003d
8 changed files with 167 additions and 51 deletions

View File

@@ -1,5 +1,16 @@
const logger = require('logger-sharelatex')
function renderJSONError(res, message, info) {
const fullInfo = { message, ...info }
if (info.message) {
logger.warn(
info,
`http error info shouldn't contain a 'message' field, will be overridden`
)
}
res.json(fullInfo)
}
module.exports = {
forbidden(req, res, message = 'restricted', info = {}) {
res.status(403)
@@ -7,16 +18,24 @@ module.exports = {
case 'html':
return res.render('user/restricted', { title: 'restricted' })
case 'json':
const fullInfo = { message, ...info }
if (info.message) {
logger.warn(
info,
`http error info shouldn't contain a 'message' field, will be overridden`
)
}
return res.json(fullInfo)
return renderJSONError(res, message, info)
default:
return res.send('restricted')
}
},
unprocessableEntity(req, res, message = 'unprocessable entity', info = {}) {
res.status(422)
switch (req.accepts(['html', 'json'])) {
case 'html':
return res.render('general/400', {
title: 'Client Error',
message: message
})
case 'json':
return renderJSONError(res, message, info)
default:
return res.send('unprocessable entity')
}
}
}