Skip to content

Commit

Permalink
Handling Error from the api-server (#83)
Browse files Browse the repository at this point in the history
Adding raise_for_status() for API-reqeusts to generate an aiohttp.ClientResponseError when server reponds with an http.response > 400
  • Loading branch information
olanystrom committed Feb 12, 2024
1 parent 90a7c3c commit fb83b58
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion custom_components/wellbeing/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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",
Expand Down

0 comments on commit fb83b58

Please sign in to comment.