-
-
Notifications
You must be signed in to change notification settings - Fork 765
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HTTP Status Code 415 not handled by custom handler #854
Comments
Hey @chinmayprabhudesai thanks for the detailed ticket. One way to do this would be to raise the correct werkzeug exceptions for 400, 415 in connexion. Unfortunately I'm not sure this would work for the other webserver (AioHttp). It actually seems like a potential problem that the connexion exceptions inherit from werkzeug. For a near-term fix, you can also register arbitrary exceptions.
You should have access to the status code in your handler, as it is a property of the ProblemException. Let me know if that works for you. I'll think on how to support status_code exception handlers without breaking aiohttp as well. |
Thank you for the response @dtkav ! Let me add some more information that I did not provide earlier (slipped my mind). I tried this before I filed the ticket, but forgot to include it. client
app.py
__init__.py
errors.py
assuming it went through all the validation and the request reached my I forked the main repo thinking the exact same that returning a response from the problem function was breaking it. So I created an object of the It later occurred to me that my request never reaches the app/api; it is handled by the validator and that is where the issue lies. The request is handled outside the scope of under-the-hood framework A good way for me (maybe a workaround for now) would be to create my own custom validator and use that to parse these requests! Edit (Jan 24, 2019):Furthermore, I noticed that RequestBodyValidator has an api, and I thought maybe that could be used, to pass the request down the line, and have something like this
But at the moment I did not understand the structure very well. I followed where
Maybe adding my @dtkav, you would be the better judge here because you understand the code base a lot better than I do, and I think if it is done this way, |
Dang! you carved out all the wrapping of
On the other side, if a 4XX error type is added to the |
Hello @slothkong,
No. It's not necessary/mandatory to implement
Not really sure about this.
You should avoid doing something like that. It defeats the purpose of the HTTP status codes. Each status code came into existence for a use case. You should use correct HTTP status codes; it's the first identifier a client would use to enforce correct responses. If you say 200 OK, but it's actually 400 Bad Request, or 401 Unauthorized or 405 Method Not Allowed; a client will be provided misleading info even though your body may indicate error messages from these exact errors. It is bad practice (and may also be against the RFC - but don't quote me on it)
Again, if you are happy with the error description provided in the underlying packages, you can just let your framework handler all errors Hope these clarify your concerns. |
Can no longer reproduce |
Description
I used OpenAPI tools to generate a Python-Connexion based server. This code-gen tool was provided an OpenAPI specification (OAS3). The OpenAPI tool gave server stubs which are bound to that spec. I am extending those for application specific response.
For starters, I decided to take care of error handling, and get that done. I wrote custom handlers for some common HTTP status codes (400, 401, 403, 404, 405, 415, 500) and used
add_error_handler
to add these error handlers to the connexion app, which in-turn registered these error handlers with the underlying Flask app (I am familiar with Flask).Most all status codes show the response from the error handler I registered, but response for HTTP status code 415 doesn't do it, and sends standard response from
connexion.problem.problem
/connexion.exception.ProblemException
.Similar case with HTTP status code 400
Expected behavior
After an incorrect media type was sent to the server, the server should use the custom handler to handle and send the following response to the client:-
Actual behavior
Shows this instead:-
Steps to reproduce
When an endpoint expecting
application/json
receives content of typetext/plain
the issue can be easily reproducedAdditional info:
errors.py
__init__.py
Edit (Jan 17, 2019):
I dug a little deeper into the implementation and found where Flask stores the error handlers -
error_handler_spec
, which is a nested dictionary created by_register_error_handler(self, key, code_or_exception, f)
called byregister_error_handler(self, code_or_exception, f)
, which itself is called byadd_error_handler
in connexion. The below code snippet shows that for 400 and 415 HTTP status codes, it does not useFlaskApp.common_error_handler
, and should use the custom error handler function. The class field is the type of HTTPException. I have also kept other response code information for referenceAs a side experiment, I ran a similar test on a pure flask application, which returned a response from the custom error handler for both HTTP status codes 400 and 415
Found the source of incorrect response I receive. It is handled by
RequestBodyValidator
inconnexion/decorators/validation.py
#459Output of the commands:
python --version
== 3.5.2pip show connexion | grep "^Version\:"
== 2.2.0The text was updated successfully, but these errors were encountered: