Skip to content

Commit

Permalink
multer error enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
Carmine DiMascio committed Dec 3, 2019
1 parent 22de592 commit c93bf9d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "dist/index.js",
"scripts": {
"compile": "rm -rf dist/ && tsc",
"test": "mocha -r source-map-support/register -r ts-node/register --recursive test/**/*.spec.ts",
"test": "TS_NODE_FILES=true mocha -r source-map-support/register -r ts-node/register --files --recursive test/**/*.spec.ts",
"test:coverage": "nyc mocha -r source-map-support/register -r ts-node/register --recursive test/**/*.spec.ts",
"coveralls": "cat coverage/lcov.info | coveralls -v",
"codacy": "cat coverage/lcov.info | codacy-coverage"
Expand Down
13 changes: 6 additions & 7 deletions src/middlewares/openapi.multipart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
OpenAPIV3,
ValidationError,
} from '../framework/types';
import { MulterError } from 'multer';
const multer = require('multer');

export function multipart(
Expand Down Expand Up @@ -74,14 +75,12 @@ function error(req: OpenApiRequest, err: Error): ValidationError {
// - 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 multerError: Express.Multer.MulterError = err;
const payload_too_big = /LIMIT_(FILE|PART)_(SIZE|COUNT)/.test(multerError.code);
const multerError = <MulterError>err;
const payload_too_big = /LIMIT_(FILE|PART)_(SIZE|COUNT)/.test(
multerError.code,
);
const unexpected = /LIMIT_UNEXPECTED_FILE/.test(multerError.code);
const status = (payload_too_big)
? 413
: (!unexpected)
? 400
: 500;
const status = payload_too_big ? 413 : !unexpected ? 400 : 500;
return validationError(status, req.path, err.message);
} else {
// HACK
Expand Down
6 changes: 3 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
"module": "commonjs",
"outDir": "dist",
"sourceMap": true,
"resolveJsonModule": true
"resolveJsonModule": true,
"typeRoots": ["./node_modules/@types", "./typings"]
},

"exclude": ["node_modules"],
"include": [
"typings.d.ts",
"typings/**/*.d.ts",
"src/**/*.ts",
"src/middlewares/modded.express.mung.ts"
]
Expand Down
25 changes: 25 additions & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import * as MulterExt from 'multer';

declare module 'multer' {
type ErrorCodes =
| 'LIMIT_PART_COUNT'
| 'LIMIT_FILE_SIZE'
| 'LIMIT_FILE_COUNT'
| 'LIMIT_FIELD_KEY'
| 'LIMIT_FIELD_VALUE'
| 'LIMIT_FIELD_COUNT'
| 'LIMIT_UNEXPECTED_FILE';

interface MulterError extends Error {
/* Constructor for MulterError */
new (code: ErrorCodes, field?: string);
/* Name of the constructor */
name: string;
/* Error Message */
message: string;
/* Error code */
code: ErrorCodes;
/* Field Name */
field?: string;
}
}

0 comments on commit c93bf9d

Please sign in to comment.