Skip to content

Commit

Permalink
Add exception handling during coordinator updates
Browse files Browse the repository at this point in the history
  • Loading branch information
basilfx committed Oct 15, 2022
1 parent 1e8a0af commit 591ca95
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions custom_components/biketrax/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import logging
from datetime import timedelta

from aiobiketrax import Account
from aiobiketrax import Account, exceptions
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
Expand Down Expand Up @@ -49,8 +49,10 @@ async def _async_update_data(self) -> None:

for device in self.account.devices:
await device.update_position()
except (HTTPError, TimeoutException) as err:
raise UpdateFailed(f"Error communicating with BikeTrax API: {err}") from err
except exceptions.BikeTraxError as err:
raise UpdateFailed(
f"A BikeTrax error occurred while updating the devices: {err}"
) from err

def start_background_task(self):
"""Start the websocket task."""
Expand Down Expand Up @@ -83,8 +85,10 @@ async def _async_update_data(self) -> None:
try:
for device in self.account.devices:
await device.update_trips()
except (HTTPError, TimeoutException) as err:
raise UpdateFailed(f"Error communicating with BikeTrax API: {err}") from err
except exceptions.BikeTraxError as err:
raise UpdateFailed(
f"A BikeTrax error occurred while updating the trips: {err}"
) from err


class SubscriptionDataUpdateCoordinator(BikeTraxDataUpdateCoordinator):
Expand All @@ -109,5 +113,7 @@ async def _async_update_data(self) -> None:
try:
for device in self.account.devices:
await device.update_subscription()
except (HTTPError, TimeoutException) as err:
raise UpdateFailed(f"Error communicating with BikeTrax API: {err}") from err
except exceptions.BikeTraxError as err:
raise UpdateFailed(
f"A BikeTrax error occurred while updating the subscription data: {err}"
) from err

0 comments on commit 591ca95

Please sign in to comment.