Refactor step 4: narrow broad except Exception catches

Replaced 28 bare `except Exception` catches across 8 files with specific
exception types reflecting the actual failure modes:

- JSON file reads → (OSError, json.JSONDecodeError)
- datetime parsing → ValueError
- base64 decoding → ValueError
- YAML parsing → (OSError, yaml.YAMLError); import moved above try
- GeoJSON coord extraction → (TypeError, IndexError, AttributeError)
- Startup temp-file cleanup → OSError
- Single JSON line parsing (SSE batch) → json.JSONDecodeError

Kept broad catches only where intentional:
- Background thread top-level guards (tasks.py, admin.py) with log.exception
- SSE stream generator tops (strava.py, garmin.py, uploads.py)
- Per-item batch loops that must not abort the whole operation
- Explicitly non-fatal post-upload merge steps with log.warning
This commit is contained in:
Davide Scaini
2026-05-13 23:58:14 +02:00
parent 8380b1d2cc
commit 27f6d141f7
9 changed files with 29 additions and 29 deletions
+6 -6
View File
@@ -32,7 +32,7 @@ def _scan_segment_for_user(dd: Path, handle: str, segment_id: str) -> int:
continue
try:
detail = json.loads(detail_path.read_text(encoding="utf-8"))
except Exception:
except (OSError, json.JSONDecodeError, ValueError):
continue
ts_url = detail.get("timeseries_url")
if not ts_url:
@@ -42,14 +42,14 @@ def _scan_segment_for_user(dd: Path, handle: str, segment_id: str) -> int:
continue
try:
ts = json.loads(ts_path.read_text(encoding="utf-8"))
except Exception:
except (OSError, json.JSONDecodeError, ValueError):
continue
started_raw = detail.get("started_at")
if not started_raw:
continue
try:
started_at = _datetime.fromisoformat(started_raw.replace("Z", "+00:00"))
except Exception:
except ValueError:
continue
track = track_from_timeseries_json(ts, detail.get("id", detail_path.stem),
detail.get("sport", "other"), started_at)
@@ -228,7 +228,7 @@ async def me_segment_rescan(
continue
try:
detail = _json.loads(detail_path.read_text(encoding="utf-8"))
except Exception:
except (OSError, _json.JSONDecodeError, ValueError):
continue
ts_url = detail.get("timeseries_url")
if not ts_url:
@@ -238,14 +238,14 @@ async def me_segment_rescan(
continue
try:
ts = _json.loads(ts_path.read_text(encoding="utf-8"))
except Exception:
except (OSError, _json.JSONDecodeError, ValueError):
continue
started_raw = detail.get("started_at")
if not started_raw:
continue
try:
started_at = _datetime.fromisoformat(started_raw.replace("Z", "+00:00"))
except Exception:
except ValueError:
continue
track = track_from_timeseries_json(
ts, detail.get("id", detail_path.stem),