From b671d4c5de71fa47c6a1d0cba7e5ad74b35627d9 Mon Sep 17 00:00:00 2001 From: Allisson Azevedo Date: Thu, 25 Jun 2020 09:53:11 -0300 Subject: [PATCH 1/2] Update ClientConnectionError to deal with httpx.NetworkError exceptions --- CHANGES.rst | 5 +++++ requirements.txt | 2 +- setup.py | 2 +- simple_rest_client/decorators.py | 13 ++++++++++--- tests/test_decorators.py | 11 ++++++++--- 5 files changed, 25 insertions(+), 8 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index f25d398..90be1e6 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,6 +1,11 @@ Changelog --------- +1.0.7 +~~~~~ + +* Update ClientConnectionError to deal with httpx.NetworkError exceptions (thanks @depauwjimmy). + 1.0.6 ~~~~~ diff --git a/requirements.txt b/requirements.txt index e94325e..77af6ab 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ -httpx>=0.13.2 +httpx>=0.13.3 python-slugify>=4.0.0 python-status>=1.0.1 diff --git a/setup.py b/setup.py index 71ae707..98ae337 100644 --- a/setup.py +++ b/setup.py @@ -25,7 +25,7 @@ changelog = f.read() -install_requirements = ["python-status>=1.0.1", "httpx>=0.13.2", "python-slugify>=4.0.0"] +install_requirements = ["python-status>=1.0.1", "httpx>=0.13.3", "python-slugify>=4.0.0"] tests_requirements = ["pytest", "pytest-asyncio", "pytest-cov", "pytest-httpserver", "coveralls"] diff --git a/simple_rest_client/decorators.py b/simple_rest_client/decorators.py index e66b45b..5f7063e 100644 --- a/simple_rest_client/decorators.py +++ b/simple_rest_client/decorators.py @@ -7,6 +7,13 @@ from .exceptions import AuthError, ClientConnectionError, ClientError, NotFoundError, ServerError logger = logging.getLogger(__name__) +client_connection_exceptions = ( + httpx.ConnectTimeout, + httpx.ReadTimeout, + httpx.WriteTimeout, + httpx.PoolTimeout, + httpx.NetworkError, +) def validate_response(response): @@ -26,9 +33,9 @@ def handle_request_error(f): def wrapper(*args, **kwargs): try: response = f(*args, **kwargs) - except (httpx.ConnectTimeout, httpx.ReadTimeout, httpx.WriteTimeout, httpx.PoolTimeout) as exc: + except client_connection_exceptions as exc: logger.exception(exc) - raise ClientConnectionError() from exc + raise ClientConnectionError(exc) validate_response(response) @@ -41,7 +48,7 @@ def handle_async_request_error(f): async def wrapper(*args, **kwargs): try: response = await f(*args, **kwargs) - except (httpx.ConnectTimeout, httpx.ReadTimeout, httpx.WriteTimeout, httpx.PoolTimeout) as exc: + except client_connection_exceptions as exc: logger.exception(exc) raise ClientConnectionError() from exc diff --git a/tests/test_decorators.py b/tests/test_decorators.py index 74235aa..6d510cd 100644 --- a/tests/test_decorators.py +++ b/tests/test_decorators.py @@ -53,7 +53,8 @@ def test_validate_response_client_error(status_code, response_kwargs): @pytest.mark.parametrize( - "side_effect", (httpx.ConnectTimeout, httpx.ReadTimeout, httpx.WriteTimeout, httpx.PoolTimeout), + "side_effect", + (httpx.ConnectTimeout, httpx.ReadTimeout, httpx.WriteTimeout, httpx.PoolTimeout, httpx.NetworkError), ) def test_handle_request_error_exceptions(side_effect): wrapped = mock.Mock(side_effect=side_effect) @@ -61,7 +62,11 @@ def test_handle_request_error_exceptions(side_effect): handle_request_error(wrapped)() -def test_handle_async_request_error_exceptions(event_loop): - wrapped = CoroutineMock(side_effect=httpx.ReadTimeout) +@pytest.mark.parametrize( + "side_effect", + (httpx.ConnectTimeout, httpx.ReadTimeout, httpx.WriteTimeout, httpx.PoolTimeout, httpx.NetworkError), +) +def test_handle_async_request_error_exceptions(event_loop, side_effect): + wrapped = CoroutineMock(side_effect=side_effect) with pytest.raises(ClientConnectionError): event_loop.run_until_complete(handle_async_request_error(wrapped)()) From bce2ce103e3e8401a16fbf97ac1e8557cdef3dce Mon Sep 17 00:00:00 2001 From: Allisson Azevedo Date: Thu, 25 Jun 2020 09:56:47 -0300 Subject: [PATCH 2/2] Fix raise on handle_async_request_error --- simple_rest_client/decorators.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/simple_rest_client/decorators.py b/simple_rest_client/decorators.py index 5f7063e..f2122a5 100644 --- a/simple_rest_client/decorators.py +++ b/simple_rest_client/decorators.py @@ -50,7 +50,7 @@ async def wrapper(*args, **kwargs): response = await f(*args, **kwargs) except client_connection_exceptions as exc: logger.exception(exc) - raise ClientConnectionError() from exc + raise ClientConnectionError(exc) validate_response(response)