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
@@ -37,7 +37,7 @@ async def list_ideas(
for path in sorted(_ideas_dir(dd).glob("*.json")):
try:
idea = json.loads(path.read_text(encoding="utf-8"))
except Exception:
except (OSError, json.JSONDecodeError):
continue
votes = idea.get("votes", [])
idea["vote_count"] = len(votes)
@@ -161,7 +161,7 @@ async def delete_idea(
raise HTTPException(404, "Not found")
try:
idea = json.loads(path.read_text(encoding="utf-8"))
except Exception:
except (OSError, json.JSONDecodeError):
raise HTTPException(500, "Could not read idea")
if not user.is_admin and idea.get("author") != user.handle:
raise HTTPException(403, "Forbidden")
@@ -221,7 +221,7 @@ async def submit_feedback(
if log_file.exists():
try:
existing = json.loads(log_file.read_text())
except Exception:
except (OSError, json.JSONDecodeError):
existing = []
existing.append(entry)
log_file.write_text(json.dumps(existing, indent=2))