From 0fb532a6ed2c053a00bcb09d9be7efbcf41405c3 Mon Sep 17 00:00:00 2001 From: Cyrill Raccaud Date: Fri, 27 Sep 2024 15:55:43 +0200 Subject: [PATCH] chain errors to be more transparent to the error source (#21) --- opendata_transport/__init__.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/opendata_transport/__init__.py b/opendata_transport/__init__.py index bdd355b..ee6f0fe 100644 --- a/opendata_transport/__init__.py +++ b/opendata_transport/__init__.py @@ -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): @@ -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.""" @@ -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"] @@ -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