Skip to content

Commit

Permalink
chain errors to be more transparent to the error source (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
miaucl authored Sep 27, 2024
1 parent 7d5fd96 commit 0fb532a
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions opendata_transport/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,18 @@ async def async_get_data(self):
_LOGGER.debug("Response from transport.opendata.ch: %s", response.status)
data = await response.json()
_LOGGER.debug(data)
except asyncio.TimeoutError:
except asyncio.TimeoutError as e:
_LOGGER.error("Can not load data from transport.opendata.ch")
raise exceptions.OpendataTransportConnectionError()
raise exceptions.OpendataTransportConnectionError() from e
except aiohttp.ClientError as aiohttpClientError:
_LOGGER.error("Response from transport.opendata.ch: %s", aiohttpClientError)
raise exceptions.OpendataTransportConnectionError()
raise exceptions.OpendataTransportConnectionError() from aiohttpClientError

try:
for station in data["stations"]:
self.locations.append(self.get_station(station))
except (KeyError, TypeError, IndexError):
raise exceptions.OpendataTransportError()
except (KeyError, TypeError, IndexError) as e:
raise exceptions.OpendataTransportError() from e


class OpendataTransportStationboard(OpendataTransportBase):
Expand Down Expand Up @@ -164,18 +164,18 @@ async def __async_get_data(self, station):
_LOGGER.debug("Response from transport.opendata.ch: %s", response.status)
data = await response.json()
_LOGGER.debug(data)
except asyncio.TimeoutError:
except asyncio.TimeoutError as e:
_LOGGER.error("Can not load data from transport.opendata.ch")
raise exceptions.OpendataTransportConnectionError()
raise exceptions.OpendataTransportConnectionError() from e
except aiohttp.ClientError as aiohttpClientError:
_LOGGER.error("Response from transport.opendata.ch: %s", aiohttpClientError)
raise exceptions.OpendataTransportConnectionError()
raise exceptions.OpendataTransportConnectionError() from aiohttpClientError

try:
for journey in data["stationboard"]:
self.journeys.append(self.get_journey(journey))
except (KeyError, TypeError, IndexError):
raise exceptions.OpendataTransportError()
except (KeyError, TypeError, IndexError) as e:
raise exceptions.OpendataTransportError() from e

async def async_get_data(self):
"""Retrieve the data for the given station."""
Expand Down Expand Up @@ -297,12 +297,12 @@ async def async_get_data(self):
_LOGGER.debug("Response from transport.opendata.ch: %s", response.status)
data = await response.json()
_LOGGER.debug(data)
except asyncio.TimeoutError:
except asyncio.TimeoutError as e:
_LOGGER.error("Can not load data from transport.opendata.ch")
raise exceptions.OpendataTransportConnectionError()
raise exceptions.OpendataTransportConnectionError() from e
except aiohttp.ClientError as aiohttpClientError:
_LOGGER.error("Response from transport.opendata.ch: %s", aiohttpClientError)
raise exceptions.OpendataTransportConnectionError()
raise exceptions.OpendataTransportConnectionError() from aiohttpClientError

try:
self.from_id = data["from"]["id"]
Expand All @@ -313,5 +313,5 @@ async def async_get_data(self):
for connection in data["connections"]:
self.connections[index] = self.get_connection(connection)
index = index + 1
except (KeyError, TypeError, IndexError):
raise exceptions.OpendataTransportError()
except (KeyError, TypeError, IndexError) as e:
raise exceptions.OpendataTransportError() from e

0 comments on commit 0fb532a

Please sign in to comment.