Closed gargkeshav closed 5 years ago
swagger-tools
never sends a response. In this case, express
is sending the response because express
has a default error handler that is doing it. You need to add an error handler to handle this yourself.
Thanks @whitlockjc for such a quick response!!!
I understand that swagger-tools never return any response and just leaves/passes it to the next middleware to handle. I believe I misstated the issue/problem statement, Allow me to put it this way.
swagger-tools
calls body-parser
to parse the request and when body-parser
fails to parse it sends an error with body-parser
format to swagger-tools
, which swagger-tools
return to next middleware/express
.
I have added the custom error handler for Swagger errors but it fails to handle this error since it's of type "entity.parse.failed".
Passing the error up as it is isn't a bad thing to do, but an error in swagger format makes more sense since body-parser
is used internally by swagger-tools
.
There are many other dependencies of swagger-tools
and handling errors of all libraries in custom error handler will be cumbersome.
your thoughts.?
I'm not sure I understand what the "swagger format" is you speak of. All errors that swagger-tools
encounters are sent downstream without modification. Now, all errors that swagger-tools
creates, there is a format but it's typically as a result of some validation error where we need to provide specific details about.
I'm all ears on how to make things better but I'm not sure swagger-tools
is being inconsistent and I'm not sure how to do things better. But if you have an idea, I'd love to hear it.
Yes, I am referring to the same format which swagger uses to provide specific details around errors encountered during validation.
How about having custom errors in swagger-tools
with the same format which swagger currently uses and process/modify errors thrown by other modules/dependencies (which are internally used by swagger for ex. body-parser
in this case) and mould it in the same format.
This way user only needs to parse/handle errors in similar format since all are being passed up by swagger-tools
.
I'm just not sure how that would look. I'll give it some thought.
When I pass an invalid structure/attribute type, swagger validator middleware throws the proper error and pass it down the flow using next which we are catching using custom error handler but when a malformed json object is passed it sends an error response with 200 HTTP status. We don't have any code that is generating this response, I tried to Debug and looks like it's happening somewhere from body-parser from within swagger middleware not sure though. Please note that there is a comma missing after first argument in request body
Request Body
response
server.js
jsonErrorFormatter
Debug Logs