feat: add proper HTTP status codes to all API error responses

- 217 error returns across 18 route files + api.py now use JSONResponse
  with appropriate HTTP status codes instead of returning HTTP 200
- Status code distribution: 500 (121), 400 (39), 503 (28), 404 (24), 409 (3), 502 (2)
- Fixed language.py tuple-return bug (was serializing as JSON array)
- Fixed bare except clauses in bipolar_mode.py and voice.py
- Body-level error schemas preserved (status/error + success/error patterns)
  so web UI continues working without changes
- chat.py (SSE) unchanged: errors sent within stream protocol
- All 170 tests pass
This commit is contained in:
2026-04-15 15:43:18 +03:00
parent 33b2033cc3
commit edc9f27925
19 changed files with 243 additions and 227 deletions

View File

@@ -2,7 +2,7 @@
import os
from fastapi import APIRouter
from fastapi.responses import FileResponse
from fastapi.responses import FileResponse, JSONResponse
from utils.logger import get_logger
logger = get_logger('api')
@@ -16,7 +16,7 @@ async def manual_image_generation(req: dict):
try:
prompt = req.get("prompt", "").strip()
if not prompt:
return {"status": "error", "message": "Prompt is required"}
return JSONResponse(status_code=400, content={"status": "error", "message": "Prompt is required"})
from utils.image_generation import generate_image_with_comfyui
image_path = await generate_image_with_comfyui(prompt)
@@ -24,10 +24,10 @@ async def manual_image_generation(req: dict):
if image_path:
return {"status": "ok", "message": f"Image generated successfully", "image_path": image_path}
else:
return {"status": "error", "message": "Failed to generate image"}
return JSONResponse(status_code=500, content={"status": "error", "message": "Failed to generate image"})
except Exception as e:
return {"status": "error", "message": f"Error: {e}"}
return JSONResponse(status_code=500, content={"status": "error", "message": f"Error: {e}"})
@router.get("/image/status")
@@ -39,7 +39,7 @@ async def get_image_generation_status():
return {"status": "ok", **status}
except Exception as e:
return {"status": "error", "message": f"Error: {e}"}
return JSONResponse(status_code=500, content={"status": "error", "message": f"Error: {e}"})
@router.post("/image/test-detection")
@@ -48,7 +48,7 @@ async def test_image_detection(req: dict):
try:
message = req.get("message", "").strip()
if not message:
return {"status": "error", "message": "Message is required"}
return JSONResponse(status_code=400, content={"status": "error", "message": "Message is required"})
from utils.image_generation import detect_image_request
is_image_request, extracted_prompt = await detect_image_request(message)
@@ -61,7 +61,7 @@ async def test_image_detection(req: dict):
}
except Exception as e:
return {"status": "error", "message": f"Error: {e}"}
return JSONResponse(status_code=500, content={"status": "error", "message": f"Error: {e}"})
@router.get("/image/view/{filename}")
@@ -88,7 +88,7 @@ async def view_generated_image(filename: str):
if not image_path:
logger.warning(f"Image not found anywhere: {filename}")
return {"status": "error", "message": f"Image not found: {filename}"}
return JSONResponse(status_code=404, content={"status": "error", "message": f"Image not found: {filename}"})
# Determine content type based on file extension
ext = filename.lower().split('.')[-1]
@@ -105,4 +105,4 @@ async def view_generated_image(filename: str):
except Exception as e:
logger.error(f"Error serving image: {e}")
return {"status": "error", "message": f"Error serving image: {e}"}
return JSONResponse(status_code=500, content={"status": "error", "message": f"Error serving image: {e}"})