connexion
connexion copied to clipboard
Exception handling - misleading documentation
Description
When reading the documentation about exception handling I felt on
Application can return (...) exceptions that inherit from both connexion.ProblemException and a werkzeug.exceptions.HttpException subclass`
Basically, I created my own custom exception this way expecting to properly returning a 404 response automatically when my exception is raised by my application.
Expected behaviour
Considering following custom exception
from werkzeug.exceptions import NotFound
from connexion import ProblemException
class ResourceNotFound(ProblemException, NotFound):
def __init__(self, resource_id: str, **kwargs):
super().__init__(**kwargs)
self.resource_id = resource_id
I expect having following response JSON data when previous exception is raised by my application
{
"detail": "The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.",
"status": 404,
"title": "Not Found",
"type": "about:blank"
}
In fact, I expected the problem exception to be "autofilled" with the http exception
Actual behaviour
Considering the same previous exception, I get the following response JSON data if my exception is raised in my app
{
"detail": null,
"status": 400,
"title": null,
"type": "about:blank"
}
Additional info:
Regarding source code, the actual behaviour looks like it is not or bug or whatever. But I think that documentation is misleading because it does not correspond to what code is actually doing about exception handling.
If I'm doing it wrong, does someone could explain me the best approach for exception handling?
Python 3.5.6 Connexion version: 2.4.0
Are you using connexion with flask or aiohttp?
Forgot to mention that, I'm using Flask
any updates on this issue?
I'm seeing similar behavior here. Extending from an additional werkzeug
exception doesn't appear to have any impact on my custom exception that extends from ProblemException
.
I'm surprised that this isn't already defined somewhere:
from connexion import ProblemException
from werkzeug.exceptions import NotFound
import http.client
class NotFoundProblem(ProblemException, NotFound):
def __init__(self, **kwargs):
assert self.code # silence type checker
d = {
"status": self.code,
"title": http.client.responses[self.code],
"detail": self.description,
}
d.update(kwargs)
super().__init__(**d)
This was reworked and documented in #1758