mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-07 20:31:06 -05:00
42c917d909
Finish o-error/http cleanup GitOrigin-RevId: 1f8cf7f1e0503d1071c51b41ac136f7fb7f38872
35 lines
803 B
JavaScript
35 lines
803 B
JavaScript
const bodyParser = require('body-parser')
|
|
const HttpErrorHandler = require('../Features/Errors/HttpErrorHandler')
|
|
|
|
function isBodyParserError(nextArg) {
|
|
if (nextArg instanceof Error) {
|
|
return (
|
|
nextArg.statusCode &&
|
|
nextArg.statusCode >= 400 &&
|
|
nextArg.statusCode < 600
|
|
)
|
|
}
|
|
return false
|
|
}
|
|
|
|
const wrapBodyParser = method => opts => {
|
|
const middleware = bodyParser[method](opts)
|
|
return (req, res, next) => {
|
|
middleware(req, res, nextArg => {
|
|
if (isBodyParserError(nextArg)) {
|
|
return HttpErrorHandler.handleErrorByStatusCode(
|
|
req,
|
|
res,
|
|
nextArg,
|
|
nextArg.statusCode
|
|
)
|
|
}
|
|
next(nextArg)
|
|
})
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
urlencoded: wrapBodyParser('urlencoded'),
|
|
json: wrapBodyParser('json')
|
|
}
|