5

I'm implementing swagger validation middleware for falcon framework. However, there's a problem with unsuccessful requests.

If a falcon HTTP error is raised in responder or even before coming to a resource (for example in case of 404 Not Found), response still has status code 200 and empty body when it comes to process_response middleware method.

I can't undertsand if it's a bug or a feature.

When I remove validation middleware, everything works as expected. When there's a swagger validation middleware, all responses in process_response have status code 200, including unsuccessful.

I want to skip swagger validation/serialization of response in case of HTTP 404, 401, 400 etc. When a response comes to process_response, how do I find out that an error was raised if the status code is 200? How do I process only successful responses in middleware?

Also, at which point does falcon set the proper response status code and body?

python 3.5, falcon==1.0.0

Any help is much appreciated.

UPD: minimal working example:

app.py:

import falcon


class Validator:
    def process_resource(self, req, resp, resource, params):
        raise falcon.HTTPBadRequest("Too bad", "All requests are bad")

    def process_response(self, req, resp, resource):
        print(resp.status, resp.body)


class Resource:
    def on_get(self, req, resp):
        pass


application = falcon.API(middleware=[Validator()])
application.add_route('/test', Resource())

$ http http http://127.0.0.1:8000/test returns:

HTTP/1.1 400 Bad Request
Connection: close
Date: Tue, 28 Jun 2016 15:09:04 GMT
Server: gunicorn/19.6.0
content-length: 69
content-type: application/json; charset=UTF-8
vary: Accept

{
    "description": "All requests are bad",
    "title": "Too bad"
}

while web app prints 200 OK None.

t_tia
  • 556
  • 1
  • 4
  • 17

0 Answers0