From 6d3a1947aff56fcee051a2dd90dce33144fbba30 Mon Sep 17 00:00:00 2001 From: Ondrej Tuma Date: Fri, 12 Jan 2024 17:03:57 +0100 Subject: [PATCH] HTTPException has status_code property * New dev release version --- doc/ChangeLog | 3 +++ poorwsgi/response.py | 8 ++++++++ poorwsgi/state.py | 4 ++-- tests/test_responses.py | 14 +++++++++++--- 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/doc/ChangeLog b/doc/ChangeLog index 0f57f6b..40e9a2c 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -1,3 +1,6 @@ +==== 2.7.0dev0 ==== + * HTTPException has status_code property + ==== 2.6.1 ==== * Fix OpenAPI Core wrappers diff --git a/poorwsgi/response.py b/poorwsgi/response.py index 3b84e5b..0806783 100644 --- a/poorwsgi/response.py +++ b/poorwsgi/response.py @@ -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. @@ -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", diff --git a/poorwsgi/state.py b/poorwsgi/state.py index 5cdf61a..485529e 100644 --- a/poorwsgi/state.py +++ b/poorwsgi/state.py @@ -8,8 +8,8 @@ import warnings __author__ = "Ondrej Tuma (McBig) " -__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 diff --git a/tests/test_responses.py b/tests/test_responses.py index 3353da4..0901bf0 100644 --- a/tests/test_responses.py +++ b/tests/test_responses.py @@ -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():