Files
Verso/services/web/app/src/infrastructure/BodyParserWrapper.mjs
T
Andrew Rumble 4f02a85aa4 Update paths
GitOrigin-RevId: 399c594dd1bbf739d91874df6be3b70e57fe01e3
2025-11-06 09:05:57 +00:00

36 lines
812 B
JavaScript

import bodyParser from 'body-parser'
import HttpErrorHandler from '../Features/Errors/HttpErrorHandler.mjs'
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 function bodyParser(req, res, next) {
middleware(req, res, nextArg => {
if (isBodyParserError(nextArg)) {
return HttpErrorHandler.handleErrorByStatusCode(
req,
res,
nextArg,
nextArg.statusCode
)
}
next(nextArg)
})
}
}
export default {
urlencoded: wrapBodyParser('urlencoded'),
json: wrapBodyParser('json'),
}