From fb83b58bf63edde9d90b5decdfc2bd61f13738c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ola=20Nystr=C3=B6m?= Date: Mon, 12 Feb 2024 14:56:52 +0100 Subject: [PATCH] Handling Error from the api-server (#83) Adding raise_for_status() for API-reqeusts to generate an aiohttp.ClientResponseError when server reponds with an http.response > 400 --- custom_components/wellbeing/api.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/custom_components/wellbeing/api.py b/custom_components/wellbeing/api.py index 03116df..47f968d 100644 --- a/custom_components/wellbeing/api.py +++ b/custom_components/wellbeing/api.py @@ -463,13 +463,16 @@ async def api_wrapper(self, method: str, url: str, data: dict = {}, headers: dic try: async with async_timeout.timeout(TIMEOUT): if method == "get": - response = await session.get(url, headers=headers) + response = await session.get(url, headers=headers, json=data) + response.raise_for_status() return await response.json() elif method == "put": response = await session.put(url, headers=headers, json=data) + response.raise_for_status() return await response.json() elif method == "post": response = await session.post(url, headers=headers, json=data) + response.raise_for_status() return await response.json() else: raise Exception("Unsupported http method '%s'" % method) @@ -487,6 +490,14 @@ async def api_wrapper(self, method: str, url: str, data: dict = {}, headers: dic url, exception, ) + except aiohttp.ClientResponseError as exception: + _LOGGER.error( + "Error, got error %s (%s) from server %s - %s", + response.status, + response.reason, + url, + exception, + ) except (aiohttp.ClientError, socket.gaierror) as exception: _LOGGER.error( "Error fetching information from %s - %s",