Skip to content

Commit

Permalink
better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
JarbasAl committed Nov 19, 2024
1 parent a9de703 commit 1ea7629
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions ovos_utils/location.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand Down

0 comments on commit 1ea7629

Please sign in to comment.