Skip to content

Commit

Permalink
Fix wrong status code and message on responses when handling `HTTPExc…
Browse files Browse the repository at this point in the history
…eption`s (#570)

* Add possible fallback to response status code in error handling

* Fix message attribute setting when handling HTTPExceptions

* Add CHANGELOG.rst entry

---------

Co-authored-by: Peter Doggart <10991724+peter-doggart@users.noreply.github.com>
  • Loading branch information
lkk7 and peter-doggart authored Dec 10, 2023
1 parent d348495 commit 25cb897
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Bug Fixes

* Fixing werkzeug 3 deprecated version import. Import is replaced by new style version check with importlib (#573) [Ryu-CZ]
* Fixing flask 3.0+ compatibility of `ModuleNotFoundError: No module named 'flask.scaffold'` Import error. (#567) [Ryu-CZ]
* Fix wrong status code and message on responses when handling `HTTPExceptions` (#569) [lkk7]


.. _section-1.2.0:
Expand Down
8 changes: 6 additions & 2 deletions flask_restx/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,9 +718,13 @@ def handle_error(self, e):
got_request_exception.send(current_app._get_current_object(), exception=e)

if isinstance(e, HTTPException):
code = HTTPStatus(e.code)
code = None
if e.code is not None:
code = HTTPStatus(e.code)
elif e.response is not None:
code = HTTPStatus(e.response.status_code)
if include_message_in_response:
default_data = {"message": getattr(e, "description", code.phrase)}
default_data = {"message": e.description or code.phrase}
headers = e.get_response().headers
elif self._default_error_handler:
result = self._default_error_handler(e)
Expand Down
19 changes: 18 additions & 1 deletion tests/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@
from flask import Blueprint, abort
from flask.signals import got_request_exception

from werkzeug.exceptions import HTTPException, BadRequest, NotFound, Aborter
from werkzeug import Response
from werkzeug.exceptions import (
Aborter,
BadRequest,
HTTPException,
NotFound,
Unauthorized,
)
from werkzeug.http import quote_etag, unquote_etag

import flask_restx as restx
Expand Down Expand Up @@ -645,6 +652,16 @@ def test_handle_error_with_code(self, app):
assert response.status_code == 500
assert json.loads(response.data.decode()) == {"foo": "bar"}

def test_handle_error_http_exception_response_code_only(self, app):
api = restx.Api(app)
http_exception = HTTPException(response=Response(status=401))

response = api.handle_error(http_exception)
assert response.status_code == 401
assert json.loads(response.data.decode()) == {
"message": "Unauthorized",
}

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

Expand Down

0 comments on commit 25cb897

Please sign in to comment.