Skip to content

Commit cb19760

Browse files
mjreissj5awry
authored andcommitted
Fix Namespace error handlers when propagate_exceptions=True
1 parent 5708758 commit cb19760

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

flask_restx/api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ def handle_error(self, e):
666666
if (
667667
not isinstance(e, HTTPException)
668668
and current_app.propagate_exceptions
669-
and not isinstance(e, tuple(self.error_handlers.keys()))
669+
and not isinstance(e, tuple(self._own_and_child_error_handlers.keys()))
670670
):
671671

672672
exc_type, exc_value, tb = sys.exc_info()

tests/test_errors.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,3 +653,30 @@ def handle_custom_exception(error):
653653
"message": "error",
654654
"test": "value",
655655
}
656+
657+
def test_namespace_errorhandler_with_propagate_true(self, app, client):
658+
"""Exceptions with errorhandler on a namespace should not be
659+
returned to client, even if PROPAGATE_EXCEPTIONS is set."""
660+
app.config["PROPAGATE_EXCEPTIONS"] = True
661+
api = restx.Api(app)
662+
namespace = restx.Namespace('test_namespace')
663+
api.add_namespace(namespace)
664+
665+
@namespace.route("/test/", endpoint="test")
666+
class TestResource(restx.Resource):
667+
def get(self):
668+
raise RuntimeError("error")
669+
670+
@namespace.errorhandler(RuntimeError)
671+
def handle_custom_exception(error):
672+
return {"message": str(error), "test": "value"}, 400
673+
674+
response = client.get("/test_namespace/test/")
675+
assert response.status_code == 400
676+
assert response.content_type == "application/json"
677+
678+
data = json.loads(response.data.decode("utf8"))
679+
assert data == {
680+
"message": "error",
681+
"test": "value",
682+
}

0 commit comments

Comments
 (0)