Skip to content

Commit 6ec2151

Browse files
authored
Add exception handling for invalid authorization (#2)
* add exception for invalid auth * add back logging self.data
1 parent 6100870 commit 6ec2151

2 files changed

Lines changed: 9 additions & 3 deletions

File tree

glances_api/__init__.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,17 @@ async def get_data(self):
5151
response = await self._session.get(url, auth=auth)
5252

5353
_LOGGER.debug("Response from Glances API: %s", response.status)
54+
response.raise_for_status()
5455
print(response.status)
5556
print(response.text)
5657
self.data = await response.json()
5758
_LOGGER.debug(self.data)
58-
except (asyncio.TimeoutError, aiohttp.ClientError):
59+
except aiohttp.ClientResponseError as err:
60+
_LOGGER.error(err.message)
61+
raise exceptions.GlancesApiAuthorizationError from err
62+
except (asyncio.TimeoutError, aiohttp.ClientError) as err:
5963
_LOGGER.error("Can not load data from Glances API")
60-
raise exceptions.GlancesApiConnectionError()
64+
raise exceptions.GlancesApiConnectionError
6165

6266
async def get_metrics(self, element):
6367
"""Get all the metrics for a monitored element."""

glances_api/exceptions.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ class GlancesApiError(Exception):
66

77
pass
88

9-
109
class GlancesApiConnectionError(GlancesApiError):
1110
"""When a connection error is encountered."""
1211

1312
pass
13+
class GlancesApiAuthorizationError(GlancesApiError):
14+
"""When a connection error is encountered."""
1415

16+
pass
1517

1618
class GlancesApiNoDataAvailable(GlancesApiError):
1719
"""When no data is available."""

0 commit comments

Comments
 (0)