From de9331ee8a1ec8793f4afbd37bbb825dbb3824a2 Mon Sep 17 00:00:00 2001 From: Petr Erastov Date: Tue, 26 Sep 2023 11:16:40 +0300 Subject: [PATCH] Fix empty exception message when timeout occurs --- cybsi/api/error.py | 7 ++++--- cybsi/api/internal/connector.py | 8 ++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/cybsi/api/error.py b/cybsi/api/error.py index 3043d9e..20d7c05 100644 --- a/cybsi/api/error.py +++ b/cybsi/api/error.py @@ -30,9 +30,10 @@ class CybsiError(Exception): """ - def __init__(self, message, ex: Optional[Exception] = None): - msg = message if ex is None else f"{message}: {ex}" - super().__init__(msg) + def __init__(self, message: str, ex: Optional[Exception] = None): + if ex and str(ex) != "": + message = f"{message}: {ex}" + super().__init__(message) self._ex = ex diff --git a/cybsi/api/internal/connector.py b/cybsi/api/internal/connector.py index 6ec6b2c..43a91b6 100644 --- a/cybsi/api/internal/connector.py +++ b/cybsi/api/internal/connector.py @@ -102,6 +102,10 @@ def _do(self, method: str, path: str, stream=False, **kwargs): resp = self._client.send(request=req, stream=stream) except CybsiError: raise + except httpx.TimeoutException as exp: + # Handle httpx.TimeoutException separately + # because it doesn't have an exception message. + raise CybsiError("request timeout", exp) from exp except Exception as exp: raise CybsiError("could not send request", exp) from exp @@ -197,6 +201,10 @@ async def _do(self, method: str, path: str, stream=False, **kwargs): resp = await self._client.send(request=req, stream=stream) except CybsiError: raise + except httpx.TimeoutException as exp: + # Handle httpx.TimeoutException separately + # because it doesn't have an exception message. + raise CybsiError("request timeout", exp) from exp except Exception as exp: raise CybsiError("could not send request", exp) from exp