From 1ea762914b2ddf716b07074dc3314736a7e13120 Mon Sep 17 00:00:00 2001 From: miro Date: Tue, 19 Nov 2024 09:51:05 +0000 Subject: [PATCH] better error handling --- ovos_utils/location.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/ovos_utils/location.py b/ovos_utils/location.py index 301929f..0e3bdfa 100644 --- a/ovos_utils/location.py +++ b/ovos_utils/location.py @@ -77,9 +77,17 @@ def get_geolocation(location: str, timeout: int = 5) -> Dict[str, Any]: return get_reverse_geolocation(lat, lon) url = "https://nominatim.openstreetmap.org/details.php" - details = requests.get(url, params={"osmid": data['osm_id'], "osmtype": data['osm_type'][0].upper(), - "format": "json"}, - headers={"User-Agent": "OVOS/1.0"}).json() + try: + response = requests.get(url, params={"osmid": data['osm_id'], "osmtype": data['osm_type'][0].upper(), + "format": "json"}, + headers={"User-Agent": "OVOS/1.0"}, timeout=timeout) + except (RequestException, Timeout) as e: + raise ConnectionError(f"Failed to connect to geolocation service: {str(e)}") + if response.status_code == 200: + details = response.json() + else: + # handle request failure + raise ConnectionError(f"Geolocation failed: status code {response.status_code}") # if no addresstags are present for the location an empty list is sent instead of a dict tags = details.get("addresstags") or {}