Skip to content

Commit

Permalink
HTTPException has status_code property
Browse files Browse the repository at this point in the history
* New dev release version
  • Loading branch information
ondratu committed Jan 12, 2024
1 parent 4f37cc4 commit 6d3a194
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
3 changes: 3 additions & 0 deletions doc/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
==== 2.7.0dev0 ====
* HTTPException has status_code property

==== 2.6.1 ====
* Fix OpenAPI Core wrappers

Expand Down
8 changes: 8 additions & 0 deletions poorwsgi/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,7 @@ class HTTPException(Exception):
>>> HTTPException(401, stale=True) # doctest: +ELLIPSIS
HTTPException(401, {'stale': True}...)
"""

def __init__(self, arg: Union[int, Response], **kwargs):
"""status_code is one of HTTP_* status code from state module.
Expand Down Expand Up @@ -801,6 +802,13 @@ def response(self):
return self.args[0]
return None

@property
def status_code(self):
"""Return status code from exception or Response."""
if isinstance(self.args[0], int):
return self.args[0]
return self.args[0].status_code


def make_response(data: Union[str, bytes],
content_type: str = "text/html; charset=utf-8",
Expand Down
4 changes: 2 additions & 2 deletions poorwsgi/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import warnings

__author__ = "Ondrej Tuma (McBig) <[email protected]>"
__date__ = "15 Dec 2023"
__version__ = "2.6.1" # https://www.python.org/dev/peps/pep-0386/
__date__ = "12 Jan 2024"
__version__ = "2.7.0dev0" # https://www.python.org/dev/peps/pep-0386/

DECLINED = 0

Expand Down
14 changes: 11 additions & 3 deletions tests/test_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,21 +438,29 @@ class TestHTTPException:
"""Tests for HTTPException and other functions which raise that."""

def test_redirect(self):
with pytest.raises(HTTPException):
with pytest.raises(HTTPException) as err:
redirect('/')

assert err.value.status_code == 302

def test_abort_status_code(self):
with pytest.raises(HTTPException) as err:
abort(404)

assert err.value.args[0] == 404
assert err.value.status_code == 404

def test_abort_response(self):
with pytest.raises(HTTPException) as err:
abort(Response(status_code=400))

assert isinstance(err.value.response, Response)
assert err.value.response.status_code == 400
assert err.value.status_code == 400

def test_ordinary_exception(self):
with pytest.raises(HTTPException) as err:
raise HTTPException(500)

assert err.value.status_code == 500


class TestFileResponse():
Expand Down

0 comments on commit 6d3a194

Please sign in to comment.