Skip to content

Do not call got_request_exception for exceptions explicitly handled #349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions flask_restx/api.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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