Skip to content

Commit a16cbf4

Browse files
Peter Doggartpeter-doggart
Peter Doggart
authored andcommitted
Fixes behaviour change bug #512
1 parent 6b3703a commit a16cbf4

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

CHANGELOG.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ Bug Fixes
3535

3636
::
3737

38-
* Update Black to 2023 version [peter_doggart]
38+
* Update Black to 2023 version [peter-doggart]
39+
* Fix minor bug introduced in 1.0.5 that changed the behaviour of how flask-restx propagates exceptions. (#512) [peter-doggart]
3940

4041
.. _section-1.0.5:
4142
1.0.5

flask_restx/api.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,10 @@ def handle_error(self, e):
685685
# client if a handler is configured for the exception.
686686
if (
687687
not isinstance(e, HTTPException)
688-
and current_app.config.get("PROPAGATE_EXCEPTIONS", False)
688+
and (
689+
current_app.config.get("PROPAGATE_EXCEPTIONS")
690+
or (current_app.testing or current_app.debug)
691+
)
689692
and not isinstance(e, tuple(self._own_and_child_error_handlers.keys()))
690693
):
691694
exc_type, exc_value, tb = sys.exc_info()

tests/test_errors.py

+42
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,48 @@ def get(self):
325325
with pytest.raises(Exception):
326326
client.get("/api/test/")
327327

328+
def test_default_errorhandler_with_propagate_not_set_but_testing(self, app, client):
329+
blueprint = Blueprint("api", __name__, url_prefix="/api")
330+
api = restx.Api(blueprint)
331+
332+
@api.route("/test/")
333+
class TestResource(restx.Resource):
334+
def get(self):
335+
raise Exception("error")
336+
337+
app.register_blueprint(blueprint)
338+
339+
app.config["PROPAGATE_EXCEPTIONS"] = None
340+
app.testing = True
341+
342+
# From the Flask docs:
343+
# PROPAGATE_EXCEPTIONS
344+
# Exceptions are re-raised rather than being handled by the app’s error handlers.
345+
# If not set, this is implicitly true if TESTING or DEBUG is enabled.
346+
with pytest.raises(Exception):
347+
client.get("/api/test/")
348+
349+
def test_default_errorhandler_with_propagate_not_set_but_debug(self, app, client):
350+
blueprint = Blueprint("api", __name__, url_prefix="/api")
351+
api = restx.Api(blueprint)
352+
353+
@api.route("/test/")
354+
class TestResource(restx.Resource):
355+
def get(self):
356+
raise Exception("error")
357+
358+
app.register_blueprint(blueprint)
359+
360+
app.config["PROPAGATE_EXCEPTIONS"] = None
361+
app.debug = True
362+
363+
# From the Flask docs:
364+
# PROPAGATE_EXCEPTIONS
365+
# Exceptions are re-raised rather than being handled by the app’s error handlers.
366+
# If not set, this is implicitly true if TESTING or DEBUG is enabled.
367+
with pytest.raises(Exception):
368+
client.get("/api/test/")
369+
328370
def test_custom_default_errorhandler(self, app, client):
329371
api = restx.Api(app)
330372

0 commit comments

Comments
 (0)