diff --git a/flask_restx/api.py b/flask_restx/api.py index 0e561f86..dd4d318d 100644 --- a/flask_restx/api.py +++ b/flask_restx/api.py @@ -666,7 +666,7 @@ def handle_error(self, e): 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())) ): exc_type, exc_value, tb = sys.exc_info() diff --git a/tests/test_errors.py b/tests/test_errors.py index 10fdc2ea..e1ce03ff 100644 --- a/tests/test_errors.py +++ b/tests/test_errors.py @@ -653,3 +653,30 @@ def handle_custom_exception(error): "message": "error", "test": "value", } + + def test_namespace_errorhandler_with_propagate_true(self, app, client): + """Exceptions with errorhandler on a namespace should not be + returned to client, even if PROPAGATE_EXCEPTIONS is set.""" + app.config["PROPAGATE_EXCEPTIONS"] = True + api = restx.Api(app) + namespace = restx.Namespace('test_namespace') + api.add_namespace(namespace) + + @namespace.route("/test/", endpoint="test") + class TestResource(restx.Resource): + def get(self): + raise RuntimeError("error") + + @namespace.errorhandler(RuntimeError) + def handle_custom_exception(error): + return {"message": str(error), "test": "value"}, 400 + + response = client.get("/test_namespace/test/") + assert response.status_code == 400 + assert response.content_type == "application/json" + + data = json.loads(response.data.decode("utf8")) + assert data == { + "message": "error", + "test": "value", + }