From df3bd36dcb92d63fab23278c45b47a44d58d2efe Mon Sep 17 00:00:00 2001 From: Jan-Philipp Benecke Date: Fri, 2 Feb 2024 20:54:39 +0100 Subject: [PATCH] Raise dedicated time out error (#38) --- aiotankerkoenig/__init__.py | 2 ++ aiotankerkoenig/aiotankerkoenig.py | 5 +++-- aiotankerkoenig/exceptions.py | 4 ++++ tests/test_aiotankerkoenig.py | 4 ++-- 4 files changed, 11 insertions(+), 4 deletions(-) diff --git a/aiotankerkoenig/__init__.py b/aiotankerkoenig/__init__.py index 2c00191..d9c9fc8 100644 --- a/aiotankerkoenig/__init__.py +++ b/aiotankerkoenig/__init__.py @@ -5,6 +5,7 @@ from .const import GasType, Sort, Status from .exceptions import ( TankerkoenigConnectionError, + TankerkoenigConnectionTimeoutError, TankerkoenigError, TankerkoenigInvalidKeyError, ) @@ -15,6 +16,7 @@ "TankerkoenigError", "TankerkoenigConnectionError", "TankerkoenigInvalidKeyError", + "TankerkoenigConnectionTimeoutError", "GasType", "Sort", "Status", diff --git a/aiotankerkoenig/aiotankerkoenig.py b/aiotankerkoenig/aiotankerkoenig.py index 81409b6..8bd13ba 100644 --- a/aiotankerkoenig/aiotankerkoenig.py +++ b/aiotankerkoenig/aiotankerkoenig.py @@ -10,7 +10,7 @@ from yarl import URL from .const import GasType, Sort -from .exceptions import TankerkoenigConnectionError, TankerkoenigError +from .exceptions import TankerkoenigConnectionTimeoutError, TankerkoenigError from .models import ( PriceInfo, PriceInfoResponse, @@ -60,9 +60,10 @@ async def _request(self, path: str, params: dict[str, Any]) -> str: url, headers=headers, ) + response.raise_for_status() except asyncio.TimeoutError as exception: msg = "Timeout occurred while connecting to tankerkoenig.de API" - raise TankerkoenigConnectionError( + raise TankerkoenigConnectionTimeoutError( msg, ) from exception diff --git a/aiotankerkoenig/exceptions.py b/aiotankerkoenig/exceptions.py index 8fbe429..0826019 100644 --- a/aiotankerkoenig/exceptions.py +++ b/aiotankerkoenig/exceptions.py @@ -10,5 +10,9 @@ class TankerkoenigConnectionError(TankerkoenigError): """Raised when a connection error occurs.""" +class TankerkoenigConnectionTimeoutError(TankerkoenigConnectionError): + """Raised when a connection times out.""" + + class TankerkoenigInvalidKeyError(TankerkoenigError): """Raised when the API key is invalid.""" diff --git a/tests/test_aiotankerkoenig.py b/tests/test_aiotankerkoenig.py index b23ef8d..b6e378b 100644 --- a/tests/test_aiotankerkoenig.py +++ b/tests/test_aiotankerkoenig.py @@ -11,7 +11,7 @@ GasType, Sort, Tankerkoenig, - TankerkoenigConnectionError, + TankerkoenigConnectionTimeoutError, TankerkoenigError, TankerkoenigInvalidKeyError, ) @@ -38,7 +38,7 @@ async def response_handler(_: str, **_kwargs: Any) -> CallbackResult: api_key="abc123", request_timeout=1, ) as tankerkoenig_client: - with pytest.raises(TankerkoenigConnectionError): + with pytest.raises(TankerkoenigConnectionTimeoutError): await tankerkoenig_client.station_details(station_id="1")