Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/middlewares/jsonBodyParserWithErrors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict'
const bodyParser = require('body-parser')

exports.jsonBodyParserWithErrors = (req, res, next) => {
bodyParser.json({ limit: '50MB' })(req, res, err => {
if (err instanceof SyntaxError || (err && err.type === 'entity.parse.failed')) {
res.status(400).json({
responseCode: 'INVALID_JSON', // response code and message can be customized here
message: 'The JSON provided in the request is invalid or malformed.',
});
} else {
next(err);
}
});
};
3 changes: 2 additions & 1 deletion src/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { routeConfigInjector } = require('@middlewares/routeConfigInjector')
const { rateLimiter } = require('@middlewares/rateLimiter')
const bodyParser = require('body-parser')
const { httpMethods } = require('@constants/httpMethods')
const {jsonBodyParserWithErrors} = require('@middlewares/jsonBodyParserWithErrors')

exports.initializeRouter = (packages) => {
try {
Expand All @@ -24,7 +25,7 @@ exports.initializeRouter = (packages) => {
targetPackagesInjector,
rateLimiter,
bodyParser.urlencoded({ extended: true, limit: '50MB' }),
bodyParser.json({ limit: '50MB' }),
jsonBodyParserWithErrors,
orchestrationController.orchestrationHandler.bind(null, packages)
)
}
Expand Down