Standardize error handling: HTTPException everywhere
Browse files
backend/api/routes/control.py
CHANGED
|
@@ -4,8 +4,7 @@ from __future__ import annotations
|
|
| 4 |
|
| 5 |
import logging
|
| 6 |
|
| 7 |
-
from fastapi import APIRouter
|
| 8 |
-
from fastapi.responses import JSONResponse
|
| 9 |
|
| 10 |
from backend.api.deps import get_datahub, get_redis_client
|
| 11 |
|
|
@@ -21,9 +20,9 @@ async def control_status():
|
|
| 21 |
data = redis.get_json("control:last_tick")
|
| 22 |
if data:
|
| 23 |
return data
|
| 24 |
-
|
| 25 |
status_code=503,
|
| 26 |
-
|
| 27 |
)
|
| 28 |
|
| 29 |
|
|
@@ -42,10 +41,10 @@ async def control_plan():
|
|
| 42 |
with open(DAILY_PLAN_PATH) as f:
|
| 43 |
return json.load(f)
|
| 44 |
except FileNotFoundError:
|
| 45 |
-
|
| 46 |
except Exception as exc:
|
| 47 |
log.error("Failed to load plan from file: %s", exc)
|
| 48 |
-
|
| 49 |
|
| 50 |
|
| 51 |
@router.get("/budget")
|
|
@@ -56,9 +55,9 @@ async def control_budget():
|
|
| 56 |
data = redis.get_json("control:budget")
|
| 57 |
if data:
|
| 58 |
return data
|
| 59 |
-
|
| 60 |
status_code=503,
|
| 61 |
-
|
| 62 |
)
|
| 63 |
|
| 64 |
|
|
@@ -71,7 +70,4 @@ async def control_trackers():
|
|
| 71 |
return {"trackers": snapshot.get("trackers", {}), "source": "ThingsBoard"}
|
| 72 |
except Exception as exc:
|
| 73 |
log.error("Tracker fetch failed: %s", exc)
|
| 74 |
-
|
| 75 |
-
status_code=502,
|
| 76 |
-
content={"error": "Tracker fetch failed"},
|
| 77 |
-
)
|
|
|
|
| 4 |
|
| 5 |
import logging
|
| 6 |
|
| 7 |
+
from fastapi import APIRouter, HTTPException
|
|
|
|
| 8 |
|
| 9 |
from backend.api.deps import get_datahub, get_redis_client
|
| 10 |
|
|
|
|
| 20 |
data = redis.get_json("control:last_tick")
|
| 21 |
if data:
|
| 22 |
return data
|
| 23 |
+
raise HTTPException(
|
| 24 |
status_code=503,
|
| 25 |
+
detail="No tick result available (worker may not have run yet)",
|
| 26 |
)
|
| 27 |
|
| 28 |
|
|
|
|
| 41 |
with open(DAILY_PLAN_PATH) as f:
|
| 42 |
return json.load(f)
|
| 43 |
except FileNotFoundError:
|
| 44 |
+
raise HTTPException(status_code=404, detail="No plan available")
|
| 45 |
except Exception as exc:
|
| 46 |
log.error("Failed to load plan from file: %s", exc)
|
| 47 |
+
raise HTTPException(status_code=500, detail="Plan loading failed")
|
| 48 |
|
| 49 |
|
| 50 |
@router.get("/budget")
|
|
|
|
| 55 |
data = redis.get_json("control:budget")
|
| 56 |
if data:
|
| 57 |
return data
|
| 58 |
+
raise HTTPException(
|
| 59 |
status_code=503,
|
| 60 |
+
detail="No budget data available",
|
| 61 |
)
|
| 62 |
|
| 63 |
|
|
|
|
| 70 |
return {"trackers": snapshot.get("trackers", {}), "source": "ThingsBoard"}
|
| 71 |
except Exception as exc:
|
| 72 |
log.error("Tracker fetch failed: %s", exc)
|
| 73 |
+
raise HTTPException(status_code=502, detail="Tracker fetch failed")
|
|
|
|
|
|
|
|
|