flask-restx icon indicating copy to clipboard operation
flask-restx copied to clipboard

@api.errorhandler doesn't work

Open lifehackett opened this issue 4 years ago • 12 comments

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

lifehackett avatar Jan 31 '20 00:01 lifehackett

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.

j5awry avatar Feb 01 '20 16:02 j5awry

@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)

nlarusstone avatar May 06 '20 18:05 nlarusstone

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.

brmzkw avatar May 06 '20 18:05 brmzkw

@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.

papasax avatar Jul 03 '20 10:07 papasax

@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.

andresDM avatar Jul 24 '20 11:07 andresDM

I looked more deeply and found this bug: In api.py, line 653:

When propagate_exceptions is set, do not return the exception to the

client if a handler is configured for the exception.

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.

papasax avatar Jul 24 '20 12:07 papasax

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 avatar Oct 25 '20 19:10 gamgi

@gamgi

Hi tried this work around but it doesn't work unfortunately.

@SteadBytes is there any plan on fixing this issue.

mittal-umang avatar Mar 08 '21 20:03 mittal-umang

There have been multiple bugfixes for errorhandling pushed in the last two releases. Is this still an issue?

j5awry avatar Aug 18 '21 12:08 j5awry

@j5awry Yes, I have still this problem.

szymonpru avatar Oct 29 '21 17:10 szymonpru

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.

StephaneConq avatar Jan 10 '22 10:01 StephaneConq

This is still an issue.

rlleshi avatar Dec 20 '23 09:12 rlleshi