aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--fnme/cli.py14
-rw-r--r--fnme/data.py21
2 files changed, 29 insertions, 6 deletions
diff --git a/fnme/cli.py b/fnme/cli.py
index 22d1a86..5437ff0 100644
--- a/fnme/cli.py
+++ b/fnme/cli.py
@@ -6,7 +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.exceptions import DataFetchError, LocationError
from fnme.geo import get_location
from fnme.station import process_stations, sort_stations
@@ -64,7 +64,17 @@ def main():
print(f"An unexpected error occurred: {e}")
sys.exit(1)
- df, last_modified = get_latest_data()
+ try:
+ df, last_modified = get_latest_data()
+ except DataFetchError as e:
+ print(f"Error: {e.message}")
+ print(
+ "Check your internet connection or verify that this script can access the cache location."
+ )
+ sys.exit(1)
+ except Exception as e:
+ print(f"An unexpected error occurred: {e}")
+ sys.exit(1)
print(f"Last updated: {last_modified}")
diff --git a/fnme/data.py b/fnme/data.py
index 6d85f43..9bb0f74 100644
--- a/fnme/data.py
+++ b/fnme/data.py
@@ -6,6 +6,7 @@ import requests
from platformdirs import user_cache_path
from fnme.constants import ENDPOINT, HEADERS
+from fnme.exceptions import DataFetchError
def get_latest_data() -> tuple[pd.DataFrame, str | None]:
@@ -28,8 +29,12 @@ def get_latest_data() -> tuple[pd.DataFrame, str | None]:
),
}
- response = requests.get(ENDPOINT, headers=conditional_headers, timeout=10)
- response.raise_for_status()
+ try:
+ response = requests.get(
+ ENDPOINT, headers=conditional_headers, timeout=10
+ )
+ except requests.RequestException as e:
+ raise DataFetchError(message=f"GET request failed: {e}")
if response.status_code == 304:
print(f"[*] Using cached data. Last modified: {cached_last_modified}")
@@ -38,6 +43,14 @@ def get_latest_data() -> tuple[pd.DataFrame, str | None]:
print("[!] Cache is stale. Refreshing.")
last_modified = response.headers.get("Last-Modified")
- csv_path.write_text(response.text, encoding="utf-8")
- timestamp_path.write_text(last_modified or "")
+
+ try:
+ csv_path.write_text(response.text, encoding="utf-8")
+ except Exception as e:
+ raise DataFetchError(message=f"Error writing CSV cache: {e}")
+ try:
+ timestamp_path.write_text(last_modified or "")
+ except Exception as e:
+ raise DataFetchError(message=f"Error writing timestamp file: {e}")
+
return pd.read_csv(csv_path), last_modified
git.ajschof.me — hosted by ajschofield — powered by cgit