diff options
| author | Alex Schofield <git@ajschof.me> | 2026-05-05 22:07:44 +0100 |
|---|---|---|
| committer | Alex Schofield <git@ajschof.me> | 2026-05-05 22:07:44 +0100 |
| commit | 736e7dabf47b7de949c55907d2d238f954a4c29e (patch) | |
| tree | 8dd04a95335fdbf2f65d9596b5e664934ad71d24 /fnme | |
| parent | c247d6e6fddb91a43b99758711912e8a6ed3e97c (diff) | |
| download | fuelnearme-736e7dabf47b7de949c55907d2d238f954a4c29e.tar.gz fuelnearme-736e7dabf47b7de949c55907d2d238f954a4c29e.zip | |
improve error handling during geolocation
Diffstat (limited to 'fnme')
| -rw-r--r-- | fnme/cli.py | 11 | ||||
| -rw-r--r-- | fnme/geo.py | 12 |
2 files changed, 19 insertions, 4 deletions
diff --git a/fnme/cli.py b/fnme/cli.py index 7c0d49d..22d1a86 100644 --- a/fnme/cli.py +++ b/fnme/cli.py @@ -6,6 +6,7 @@ from tabulate import tabulate from fnme.constants import SORT_KV from fnme.data import get_latest_data +from fnme.exceptions import LocationError from fnme.geo import get_location from fnme.station import process_stations, sort_stations @@ -54,9 +55,15 @@ def main(): try: location = get_location(args.address) - except ValueError as e: - print(f"[*] {e}") + print(f"[✔] Coordinates found: {args.address}") + except LocationError as e: + print(f"Error: {e.message}") + print("Check the spelling of the address or try a different address.") sys.exit(1) + except Exception as e: + print(f"An unexpected error occurred: {e}") + sys.exit(1) + df, last_modified = get_latest_data() print(f"Last updated: {last_modified}") diff --git a/fnme/geo.py b/fnme/geo.py index dba68fe..ec10237 100644 --- a/fnme/geo.py +++ b/fnme/geo.py @@ -1,10 +1,18 @@ +from geopy import exc from geopy.geocoders import Nominatim from geopy.location import Location +from fnme.exceptions import LocationError + def get_location(address: str) -> tuple[float, float]: geolocator = Nominatim(user_agent="FuelNearMe") - result = geolocator.geocode(address) + + try: + result = geolocator.geocode(address) + except exc.GeopyError as e: + raise LocationError(message=f"Location service error: {e}") + if not isinstance(result, Location): - raise ValueError(f"Failed to get location from address: '{address}'") + raise LocationError(message=f"Unknown location: '{address}'") return (result.latitude, result.longitude) |
