Skip to content

Commit a61c4fc

Browse files
committed
Improve exception handling
1 parent 8c77a81 commit a61c4fc

File tree

1 file changed

+20
-23
lines changed

1 file changed

+20
-23
lines changed

airos/airos8.py

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from __future__ import annotations
44

5+
import asyncio
56
import json
67
import logging
78
from typing import Any
@@ -185,13 +186,12 @@ async def login(self) -> bool:
185186
log = f"Login failed with status {response.status}. Full Response: {response.text}"
186187
_LOGGER.error(log)
187188
raise AirOSConnectionAuthenticationError from None
188-
except (
189-
aiohttp.ClientError,
190-
aiohttp.client_exceptions.ConnectionTimeoutError,
191-
aiohttp.client_exceptions.ClientConnectorError,
192-
) as err:
189+
except (TimeoutError, aiohttp.ClientError) as err:
193190
_LOGGER.exception("Error during login")
194191
raise AirOSDeviceConnectionError from err
192+
except asyncio.CancelledError:
193+
_LOGGER.info("Login task was cancelled")
194+
raise
195195

196196
def derived_data(
197197
self, response: dict[str, Any] | None = None
@@ -302,13 +302,12 @@ async def status(self) -> AirOSData:
302302
response_text,
303303
)
304304
raise AirOSDeviceConnectionError
305-
except (
306-
aiohttp.ClientError,
307-
aiohttp.client_exceptions.ConnectionTimeoutError,
308-
aiohttp.client_exceptions.ClientConnectorError,
309-
) as err:
310-
_LOGGER.error("Status API call failed: %s", err)
305+
except (TimeoutError, aiohttp.ClientError) as err:
306+
_LOGGER.exception("Status API call failed: %s", err)
311307
raise AirOSDeviceConnectionError from err
308+
except asyncio.CancelledError:
309+
_LOGGER.info("API status retrieval task was cancelled")
310+
raise
312311

313312
async def stakick(self, mac_address: str = None) -> bool:
314313
"""Reconnect client station."""
@@ -341,13 +340,12 @@ async def stakick(self, mac_address: str = None) -> bool:
341340
log = f"Unable to restart connection response status {response.status} with {response_text}"
342341
_LOGGER.error(log)
343342
return False
344-
except (
345-
aiohttp.ClientError,
346-
aiohttp.client_exceptions.ConnectionTimeoutError,
347-
aiohttp.client_exceptions.ClientConnectorError,
348-
) as err:
349-
_LOGGER.exception("Error during reconnect request call")
343+
except (TimeoutError, aiohttp.ClientError) as err:
344+
_LOGGER.exception("Error during call to reconnect remote: %s", err)
350345
raise AirOSDeviceConnectionError from err
346+
except asyncio.CancelledError:
347+
_LOGGER.info("Reconnect task was cancelled")
348+
raise
351349

352350
async def provmode(self, active: bool = False) -> bool:
353351
"""Set provisioning mode."""
@@ -381,10 +379,9 @@ async def provmode(self, active: bool = False) -> bool:
381379
log = f"Unable to change provisioning mode response status {response.status} with {response_text}"
382380
_LOGGER.error(log)
383381
return False
384-
except (
385-
aiohttp.ClientError,
386-
aiohttp.client_exceptions.ConnectionTimeoutError,
387-
aiohttp.client_exceptions.ClientConnectorError,
388-
) as err:
389-
_LOGGER.exception("Error during provisioning mode call")
382+
except (TimeoutError, aiohttp.ClientError) as err:
383+
_LOGGER.exception("Error during call to change provisioning mode: %s", err)
390384
raise AirOSDeviceConnectionError from err
385+
except asyncio.CancelledError:
386+
_LOGGER.info("Provisioning mode change task was cancelled")
387+
raise

0 commit comments

Comments
 (0)