Fix httpclient exceptions on read and improve error info (#655)

This commit is contained in:
Steven B
2024-01-19 20:06:50 +00:00
committed by GitHub
parent 0647adaba0
commit 38159140fb
3 changed files with 121 additions and 11 deletions

View File

@@ -1,4 +1,5 @@
"""Module for HttpClientSession class."""
import asyncio
from typing import Any, Dict, Optional, Tuple, Union
import aiohttp
@@ -58,25 +59,27 @@ class HttpClient:
cookies=cookies_dict,
headers=headers,
)
async with resp:
if resp.status == 200:
response_data = await resp.read()
if json:
response_data = json_loads(response_data.decode())
except (aiohttp.ServerDisconnectedError, aiohttp.ClientOSError) as ex:
raise ConnectionException(
f"Unable to connect to the device: {self._config.host}: {ex}"
f"Unable to connect to the device: {self._config.host}: {ex}", ex
) from ex
except aiohttp.ServerTimeoutError as ex:
except (aiohttp.ServerTimeoutError, asyncio.TimeoutError) as ex:
raise TimeoutException(
"Unable to query the device, " + f"timed out: {self._config.host}: {ex}"
"Unable to query the device, "
+ f"timed out: {self._config.host}: {ex}",
ex,
) from ex
except Exception as ex:
raise SmartDeviceException(
f"Unable to query the device: {self._config.host}: {ex}"
f"Unable to query the device: {self._config.host}: {ex}", ex
) from ex
async with resp:
if resp.status == 200:
response_data = await resp.read()
if json:
response_data = json_loads(response_data.decode())
return resp.status, response_data
def get_cookie(self, cookie_name: str) -> Optional[str]: