Open lifehackett opened 4 years ago
We're going to need more info. 693 was closed with the fix (which our first release should have). 764 was mentioned as a dupe by the opener of 693
It should be fixed in our codeline from commit https://github.com/noirbizarre/flask-restplus/commit/de38220655d159a1dc43fd9a305a6903f62c6e52
if you are still seeing the issue, please provide more information.
@brmzkw as you commented in https://github.com/noirbizarre/flask-restplus/issues/693#issuecomment-603113736, this issue does not appear to be fixed (here's the corresponding flask-restx issue)
Thanks for the highlight. I'm removing flask-restx from my projects, but feel free to ask if I can provide more help or details.
@j5awry : Here is some more info. Apparently this bug only occurs when FLASK is in DEBUG mode. If you turn this off everything is fine.
@j5awry Thanks for the framework, it is super useful! Some extra info regarding this topic:
In my case the errorhandler don't work in a name space when implementing blueprints as in: Multiple APIs with reusable namespaces.
I do not face this problem when registering the handlers in the Blueprint or the Api object. I think registering in the namespaces is quite useful though.
I looked more deeply and found this bug: In api.py, line 653:
The tests forget to check the child error handler if ( not isinstance(e, HTTPException) and current_app.propagate_exceptions and not isinstance(e, tuple(self.error_handlers.keys())) )
should include the childs: if ( not isinstance(e, HTTPException) and current_app.propagate_exceptions and not isinstance(e, tuple(self.error_handlers.keys())) and not isinstance(e, tuple(self.own_and_child_error_handlers.keys())) )
But it does not fix everything, since if 1 exception handler is registered in 2 different namespace, it will always call the first.
It's possible to work around this bug by manually setting api.error_handlers
.
Example namespaced API
# api/my_api.py
from flask_restx import Namespace, Resource
api = Namespace("my_api")
@api.route('/foo')
class MyResource(Resource):
def get(self):
raise RuntimeError('bar')
and then
# api/__init__.py
from flask import Blueprint
from flask_restx import Api
from .my_api import api as my_api
blueprint = Blueprint('api', __name__)
api = Api(blueprint, title='My Title')
api.add_namespace(my_api)
@api.errorhandler
def handle_exception(error: Exception):
"""When an unhandled exception is raised"""
message = "Error: " + getattr(error, 'message', str(error))
return {'message': message}, getattr(error, 'code', 500)
# error handlers should work with @api.errorhandler, as per https://flask-restx.readthedocs.io/en/latest/errors.html
# but there is a bug, this is a workaround for https://github.com/python-restx/flask-restx/issues/27
api._default_error_handler = Exception
api.error_handlers[Exception] = handle_exception
@gamgi
Hi tried this work around but it doesn't work unfortunately.
@SteadBytes is there any plan on fixing this issue.
There have been multiple bugfixes for errorhandling pushed in the last two releases. Is this still an issue?
@j5awry Yes, I have still this problem.
Hi,
Not sure if it helps, but I had the same issue and fixed it. My files were organized like so:
controller.py # list of all routes
service.py # list of functions called by the controller to handle logic
exceptions.py # list of my custom exceptions
error_handlers.py # error handlers declared with @api.errorhandler
In my service.py
, I used to import the exceptions I wanted to raise from exceptions.py
. But that way, we never went through the error_handlers.
I fixed it by importing my exceptions from error_handlers.py
.
Hope this helps someone.
This is still an issue.
Thank you for forking this project. It does not seem like this issue is addressed yet nor did I see an existing issue mentioning it, so porting it over to this project. I believe these 3 issues capture the problem well enough, but if you need additional information, just shout.
https://github.com/noirbizarre/flask-restplus/issues/764 https://github.com/noirbizarre/flask-restplus/issues/744 https://github.com/noirbizarre/flask-restplus/issues/693
BTW, the work around using
PROPOPGATE_EXCEPTIONS
did not work for me.Thanks