From eaf0d43311f5f188ff6219f16202e551205c9de1 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 18 Jan 2026 13:28:22 +0000 Subject: [PATCH] Add error handling for cache file operations Co-authored-by: kuhyx <147418882+kuhyx@users.noreply.github.com> --- .../fetch_license_plates.py | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/python_pkg/polish_license_plates/fetch_license_plates.py b/python_pkg/polish_license_plates/fetch_license_plates.py index 43f7025..eb46de3 100755 --- a/python_pkg/polish_license_plates/fetch_license_plates.py +++ b/python_pkg/polish_license_plates/fetch_license_plates.py @@ -104,10 +104,15 @@ def fetch_wikipedia_html(*, force_refresh: bool = False) -> str: # Check if we can use cache if not force_refresh and is_cache_valid(cache_path): - sys.stdout.write(f"Using cached data from {cache_path}\n") - cache_age_hours = int((time.time() - cache_path.stat().st_mtime) / 3600) - sys.stdout.write(f"Cache age: {cache_age_hours} hours\n") - return cache_path.read_text(encoding="utf-8") + try: + sys.stdout.write(f"Using cached data from {cache_path}\n") + cache_age_hours = int((time.time() - cache_path.stat().st_mtime) / 3600) + sys.stdout.write(f"Cache age: {cache_age_hours} hours\n") + return cache_path.read_text(encoding="utf-8") + except OSError as e: + sys.stderr.write(f"Warning: Failed to read cache: {e}\n") + sys.stderr.write("Fetching fresh data from Wikipedia...\n") + # Fall through to fetch from Wikipedia # Fetch from Wikipedia url = "https://en.wikipedia.org/wiki/Vehicle_registration_plates_of_Poland" @@ -126,8 +131,12 @@ def fetch_wikipedia_html(*, force_refresh: bool = False) -> str: raise RuntimeError(msg) from e # Cache the response - cache_path.write_text(response.text, encoding="utf-8") - sys.stdout.write(f"Cached response to {cache_path}\n") + try: + cache_path.write_text(response.text, encoding="utf-8") + sys.stdout.write(f"Cached response to {cache_path}\n") + except OSError as e: + sys.stderr.write(f"Warning: Failed to write cache: {e}\n") + # Continue anyway - the data was fetched successfully return response.text