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
+3 -3
View File
@@ -42,7 +42,7 @@ def _upsert_index_summary(user_dir: Path, activity_id: str, activity: dict, geoj
if coords:
step = max(1, len(coords) // 9)
preview = [[c[1], c[0]] for c in coords[::step]][:9]
except Exception:
except (TypeError, IndexError, AttributeError):
pass
has_track = (user_dir / "activities" / f"{activity_id}.geojson").exists()
@@ -183,7 +183,7 @@ async def upload_raw_activity(
try:
raw = _b64.b64decode(b64)
except Exception:
except ValueError:
raise HTTPException(400, "Invalid base64 encoding")
source_hash = hashlib.sha256(raw).hexdigest()
@@ -378,7 +378,7 @@ async def upload_activity(
cache = json.loads(cache_path.read_text(encoding="utf-8"))
cache.pop(activity_id, None)
cache_path.write_text(json.dumps(cache, ensure_ascii=False))
except Exception:
except (OSError, json.JSONDecodeError):
pass
# Remove merged copies (merge_all will regenerate them after ingest)
merged_acts = dd / "_merged" / "activities"