Skip to content

Commit

Permalink
Merge pull request #349 from VolkaRacho/do_not_always_call_got_reques…
Browse files Browse the repository at this point in the history
…t_exception

Do not call got_request_exception for exceptions explicitly handled contrib: VolkaRancho, chandlernine review: j5awry
  • Loading branch information
j5awry committed Jul 8, 2021
2 parents 0dc1c3c + 872cc97 commit 596e97d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
6 changes: 4 additions & 2 deletions flask_restx/api.py
Expand Up @@ -679,8 +679,6 @@ def handle_error(self, e):
:param Exception e: the raised Exception object
"""
got_request_exception.send(current_app._get_current_object(), exception=e)

# When propagate_exceptions is set, do not return the exception to the
# client if a handler is configured for the exception.
if (
Expand Down Expand Up @@ -710,6 +708,10 @@ def handle_error(self, e):
)
break
else:
# Flask docs say: "This signal is not sent for HTTPException or other exceptions that have error handlers
# registered, unless the exception was raised from an error handler."
got_request_exception.send(current_app._get_current_object(), exception=e)

if isinstance(e, HTTPException):
code = HTTPStatus(e.code)
if include_message_in_response:
Expand Down
21 changes: 21 additions & 0 deletions tests/test_errors.py
Expand Up @@ -444,6 +444,27 @@ def record(sender, exception):
finally:
got_request_exception.disconnect(record, app)

def test_handle_error_signal_does_not_call_got_request_exception(self, app):
api = restx.Api(app)

exception = BadRequest()

recorded = []

def record(sender, exception):
recorded.append(exception)

@api.errorhandler(BadRequest)
def handle_bad_request(error):
return {"message": str(error), "value": "test"}, 400

got_request_exception.connect(record, app)
try:
api.handle_error(exception)
assert len(recorded) == 0
finally:
got_request_exception.disconnect(record, app)

def test_handle_error(self, app):
api = restx.Api(app)

Expand Down

0 comments on commit 596e97d

Please sign in to comment.