flask-restx
flask-restx copied to clipboard
Error_handler when DEBUG MODE = ON and PROPAGATE_EXCEPTIONS = True Broken ?
***** BEFORE LOGGING AN ISSUE *****
- Is this something you can debug and fix? Send a pull request! Bug fixes and documentation fixes are welcome.
- Please check if a similar issue already exists or has been closed before. Seriously, nobody here is getting paid. Help us out and take five minutes to make sure you aren't submitting a duplicate.
- Please review the guidelines for contributing
Code
# set PROPAGATE_EXCEPTIONS = True
# set DEBUG MODE = ON
class CustomException(Exception):
def __init__(self, code, message):
self.message = message
self.code = code
@api.errorhandler(CustomException)
def handle_custom_exception(error):
print("Debug custom exception")
return {}, 666
@api.route("/<int:scenario_id>")
@api.response(404, "Scenario not found")
@api.param("scenario_id", "The scenario identifier")
class RouteScenario(Resource):
decorators = [is_owner]
@api.doc("get_scenario")
@api.marshal_with(scenario)
def get(self, scenario_id: int):
raise CustomException(400, "CustomException catched")
Repro Steps (if applicable)
set PROPAGATE_EXCEPTIONS = True and Debug mode ON
- Create an exception inherited from Exception
- Create errorhandler of this exception wich will return a different error code than the exception / print debug message on the errorhandler function
- Create simple endpoint and raised the exception
- Hit the endpoint with postman or equivalent
Expected Behavior
A description of what you expected to happen.
I should get the error code wrote on the handler and the debug message on the error_handler function should be displayed
Actual Behavior
A description of the unexpected, buggy behavior.
I got the HTTP error 500 and the error_handler function is not called
Error Messages/Stack Trace
If applicable, add the stack trace produced by the error 127.0.0.1 - - [04/May/2021 15:27:04] "GET /scenario/2 HTTP/1.1" 500 - Traceback (most recent call last): File "/home/elias/.local/share/virtualenvs/backend-bAJgLDCZ/lib/python3.8/site-packages/flask/app.py", line 2464, in call return self.wsgi_app(environ, start_response) File "/home/elias/.local/share/virtualenvs/backend-bAJgLDCZ/lib/python3.8/site-packages/flask/app.py", line 2450, in wsgi_app response = self.handle_exception(e) File "/home/elias/.local/share/virtualenvs/backend-bAJgLDCZ/lib/python3.8/site-packages/flask_restx/api.py", line 638, in error_router return original_handler(f) File "/home/elias/.local/share/virtualenvs/backend-bAJgLDCZ/lib/python3.8/site-packages/flask_cors/extension.py", line 165, in wrapped_function return cors_after_request(app.make_response(f(*args, **kwargs))) File "/home/elias/.local/share/virtualenvs/backend-bAJgLDCZ/lib/python3.8/site-packages/flask/app.py", line 1867, in handle_exception reraise(exc_type, exc_value, tb) File "/home/elias/.local/share/virtualenvs/backend-bAJgLDCZ/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise raise value File "/home/elias/.local/share/virtualenvs/backend-bAJgLDCZ/lib/python3.8/site-packages/flask_restx/api.py", line 636, in error_router return self.handle_error(e) File "/home/elias/.local/share/virtualenvs/backend-bAJgLDCZ/lib/python3.8/site-packages/flask/app.py", line 2447, in wsgi_app response = self.full_dispatch_request() File "/home/elias/.local/share/virtualenvs/backend-bAJgLDCZ/lib/python3.8/site-packages/flask/app.py", line 1952, in full_dispatch_request rv = self.handle_user_exception(e) File "/home/elias/.local/share/virtualenvs/backend-bAJgLDCZ/lib/python3.8/site-packages/flask_restx/api.py", line 638, in error_router return original_handler(f) File "/home/elias/.local/share/virtualenvs/backend-bAJgLDCZ/lib/python3.8/site-packages/flask_cors/extension.py", line 165, in wrapped_function return cors_after_request(app.make_response(f(*args, **kwargs))) File "/home/elias/.local/share/virtualenvs/backend-bAJgLDCZ/lib/python3.8/site-packages/flask/app.py", line 1821, in handle_user_exception reraise(exc_type, exc_value, tb) File "/home/elias/.local/share/virtualenvs/backend-bAJgLDCZ/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise raise value File "/home/elias/.local/share/virtualenvs/backend-bAJgLDCZ/lib/python3.8/site-packages/flask_restx/api.py", line 636, in error_router return self.handle_error(e) File "/home/elias/.local/share/virtualenvs/backend-bAJgLDCZ/lib/python3.8/site-packages/flask/app.py", line 1950, in full_dispatch_request rv = self.dispatch_request() File "/home/elias/.local/share/virtualenvs/backend-bAJgLDCZ/lib/python3.8/site-packages/flask/app.py", line 1936, in dispatch_request return self.view_functionsrule.endpoint File "/home/elias/.local/share/virtualenvs/backend-bAJgLDCZ/lib/python3.8/site-packages/flask_restx/api.py", line 375, in wrapper resp = resource(*args, **kwargs) File "/home/elias/stage/e-demain/backend/energy_diag/auth.py", line 108, in wrapper return fn(*args, **kwargs) File "/home/elias/.local/share/virtualenvs/backend-bAJgLDCZ/lib/python3.8/site-packages/flask/views.py", line 89, in view return self.dispatch_request(*args, **kwargs) File "/home/elias/.local/share/virtualenvs/backend-bAJgLDCZ/lib/python3.8/site-packages/flask_restx/resource.py", line 44, in dispatch_request resp = meth(*args, **kwargs) File "/home/elias/.local/share/virtualenvs/backend-bAJgLDCZ/lib/python3.8/site-packages/flask_restx/marshalling.py", line 248, in wrapper resp = f(*args, **kwargs) File "/home/elias/stage/e-demain/backend/energy_diag/routes/scenarios.py", line 90, in get raise CustomException(400, "CustomException catched") energy_diag.routes.scenarios.CustomException: (400, 'CustomException catched')
Environment
Python 3.8.9 Flask 1.1.2 flask-marshmallow 0.14.0 flask-restx 0.3.0 Flask-SQLAlchemy 2.5.1 Flask-JWT-Extended 4.2.0
Additional Context
It works with PROPAGATE_EXCEPTION on False. But if I understood the code https://flask-restx.readthedocs.io/en/latest/_modules/flask_restx/api.html#Api.handle_error this is not the right behavior
Is there any progress on this issue