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:
@@ -1,6 +1,7 @@
|
||||
"""Evil mode routes."""
|
||||
|
||||
from fastapi import APIRouter
|
||||
from fastapi.responses import JSONResponse
|
||||
import globals
|
||||
from routes.models import EvilMoodSetRequest
|
||||
from utils.logger import get_logger
|
||||
@@ -43,7 +44,7 @@ def enable_evil_mode():
|
||||
globals.client.loop.create_task(apply_evil_mode_changes(globals.client))
|
||||
return {"status": "ok", "message": "Evil mode enabled", "evil_mode": True}
|
||||
else:
|
||||
return {"status": "error", "message": "Discord client not ready"}
|
||||
return JSONResponse(status_code=503, content={"status": "error", "message": "Discord client not ready"})
|
||||
|
||||
|
||||
@router.post("/evil-mode/disable")
|
||||
@@ -58,7 +59,7 @@ def disable_evil_mode():
|
||||
globals.client.loop.create_task(revert_evil_mode_changes(globals.client))
|
||||
return {"status": "ok", "message": "Evil mode disabled", "evil_mode": False}
|
||||
else:
|
||||
return {"status": "error", "message": "Discord client not ready"}
|
||||
return JSONResponse(status_code=503, content={"status": "error", "message": "Discord client not ready"})
|
||||
|
||||
|
||||
@router.post("/evil-mode/toggle")
|
||||
@@ -67,7 +68,7 @@ def toggle_evil_mode():
|
||||
from utils.evil_mode import apply_evil_mode_changes, revert_evil_mode_changes
|
||||
|
||||
if not globals.client or not globals.client.loop or not globals.client.loop.is_running():
|
||||
return {"status": "error", "message": "Discord client not ready"}
|
||||
return JSONResponse(status_code=503, content={"status": "error", "message": "Discord client not ready"})
|
||||
|
||||
if globals.EVIL_MODE:
|
||||
globals.client.loop.create_task(revert_evil_mode_changes(globals.client))
|
||||
@@ -95,10 +96,10 @@ def set_evil_mood_endpoint(data: EvilMoodSetRequest):
|
||||
from utils.evil_mode import set_evil_mood, is_valid_evil_mood, update_all_evil_nicknames
|
||||
|
||||
if not is_valid_evil_mood(data.mood):
|
||||
return {
|
||||
return JSONResponse(status_code=400, content={
|
||||
"status": "error",
|
||||
"message": f"Mood '{data.mood}' not recognized. Available evil moods: {', '.join(globals.EVIL_AVAILABLE_MOODS)}"
|
||||
}
|
||||
})
|
||||
|
||||
success = set_evil_mood(data.mood)
|
||||
if success:
|
||||
@@ -107,4 +108,4 @@ def set_evil_mood_endpoint(data: EvilMoodSetRequest):
|
||||
globals.client.loop.create_task(update_all_evil_nicknames(globals.client))
|
||||
return {"status": "ok", "new_mood": data.mood}
|
||||
|
||||
return {"status": "error", "message": "Failed to set evil mood"}
|
||||
return JSONResponse(status_code=500, content={"status": "error", "message": "Failed to set evil mood"})
|
||||
|
||||
Reference in New Issue
Block a user