diff --git a/gateway/main.py b/gateway/main.py index cffac96..5805e3d 100644 --- a/gateway/main.py +++ b/gateway/main.py @@ -61,16 +61,23 @@ async def _get_health(client: httpx.AsyncClient, base: str) -> Dict[str, Any]: async def _post_json(client: httpx.AsyncClient, url: str, payload: Dict[str, Any]) -> Dict[str, Any]: r = await client.post(url, json=payload) if r.status_code >= 400: - raise HTTPException(status_code=502, detail=f"Upstream error {url}: {r.status_code} {r.text[:200]}") - return r.json() + raise HTTPException(status_code=502, detail=f"Upstream error {url}: {r.status_code} {r.text[:1000]}") + try: + return r.json() + except Exception: + # upstream returned non-JSON (HTML error page or empty body) + raise HTTPException(status_code=502, detail=f"Upstream returned non-JSON at {url}: {r.status_code} {r.text[:1000]}") async def _post_file(client: httpx.AsyncClient, url: str, data: bytes, fields: Dict[str, Any]) -> Dict[str, Any]: files = {"file": ("image", data, "application/octet-stream")} r = await client.post(url, data={k: str(v) for k, v in fields.items()}, files=files) if r.status_code >= 400: - raise HTTPException(status_code=502, detail=f"Upstream error {url}: {r.status_code} {r.text[:200]}") - return r.json() + raise HTTPException(status_code=502, detail=f"Upstream error {url}: {r.status_code} {r.text[:1000]}") + try: + return r.json() + except Exception: + raise HTTPException(status_code=502, detail=f"Upstream returned non-JSON at {url}: {r.status_code} {r.text[:1000]}") @app.get("/health")