From aa67bc32442e15e46654fb62d02b4a4daf723331 Mon Sep 17 00:00:00 2001 From: Jacques Yakoub Date: Mon, 2 Dec 2019 21:49:48 +0100 Subject: [PATCH] Better handling of MulterError --- src/middlewares/openapi.multipart.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/middlewares/openapi.multipart.ts b/src/middlewares/openapi.multipart.ts index 6a569faa..1c1b1aa8 100644 --- a/src/middlewares/openapi.multipart.ts +++ b/src/middlewares/openapi.multipart.ts @@ -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