Skip to content

Commit

Permalink
enhance error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Carmine DiMascio committed May 24, 2019
1 parent 4e3c5fd commit fc2f98b
Showing 1 changed file with 41 additions and 21 deletions.
62 changes: 41 additions & 21 deletions src/middlewares/openapi.multipart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,26 @@ export function multipart(openApiContext: OpenApiContext, multerOpts: {} = {}) {
}
mult.any()(req, res, err => {
if (err) {
if (err instanceof multer.MulterError) {
// TODO is special handling for MulterErrors needed
console.error(err);
next(validationError(500, req.path, err.message));
} else {
// HACK
// TODO improve multer error handling
const missingField = /Multipart: Boundary not found/i.test(
err.message || '',
);
if (missingField) {
next(validationError(400, req.path, 'multipart file(s) required.'))
} else {
console.error(err);
next(validationError(500, req.path, err.message));
}
}
next(error(req, err));
} else {
req.files.forEach(f => {
req.body[f.fieldname] = '';
});
// TODO:
// If a form parameter 'file' is defined to take file value, but the user provides a string value instead
// req.files will be empty and req.body.file will be populated with a string
// This will incorrectly PASS validation.
// Instead, we should return a 400 with an invalid type e.g. file expects a file, but found string.
//
// In order to support this, we likely need to inspect the schema directly to find the type.
// For example, if param with type: 'string', format: 'binary' is defined, we expect to see it in
// req.files. If it's not present we should throw a 400
//
// This is a bit complex because the schema may be defined inline (easy) or via a $ref (complex) in which
// case we must follow the $ref to check the type.
if (req.files) {
// add files to body
req.files.forEach(f => {
req.body[f.fieldname] = '';
});
}
next();
}
});
Expand All @@ -42,7 +41,8 @@ export function multipart(openApiContext: OpenApiContext, multerOpts: {} = {}) {
}

function isValidContentType(req) {
return req.headers['content-type'].includes('multipart/form-data');
const contentType = req.headers['content-type'];
return !contentType || contentType.includes('multipart/form-data');
}

function isMultipart(req) {
Expand All @@ -54,3 +54,23 @@ function isMultipart(req) {
req.openapi.schema.requestBody.content['multipart/form-data']
);
}

function error(req, err) {
if (err instanceof multer.MulterError) {
// TODO is special handling for MulterErrors needed
console.error(err);
return validationError(500, req.path, err.message);
} else {
// HACK
// TODO improve multer error handling
const missingField = /Multipart: Boundary not found/i.test(
err.message || '',
);
if (missingField) {
return validationError(400, req.path, 'multipart file(s) required.');
} else {
console.error(err);
return validationError(500, req.path, err.message);
}
}
}

0 comments on commit fc2f98b

Please sign in to comment.