Skip to content

Commit

Permalink
Better handling of MulterError
Browse files Browse the repository at this point in the history
  • Loading branch information
jy95 committed Dec 2, 2019
1 parent 1b3c49a commit aa67bc3
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/middlewares/openapi.multipart.ts
Expand Up @@ -70,7 +70,17 @@ function isMultipart(req: OpenApiRequest): boolean {

function error(req: OpenApiRequest, err: Error): ValidationError {
if (err instanceof multer.MulterError) {
// TODO is special handling for MulterErrors needed
// distinguish common errors :
// - 413 ( Request Entity Too Large ) : Too many parts / File too large / Too many files
// - 400 ( Bad Request ) : Field * too long / Too many fields
// - 500 ( Internal Server Error ) : Unexpected field
const payload_too_big = /LIMIT_(FILE|PART)_(SIZE|COUNT)/.test(err.code);
const unexpected = /LIMIT_UNEXPECTED_FILE/.test(err.code);
const status = (payload_too_big)
? 413
: (!unexpected)
? 400
: 500;
return validationError(500, req.path, err.message);
} else {
// HACK
Expand Down

0 comments on commit aa67bc3

Please sign in to comment.