fix(ai): risposta AI malformata -> 502, non 400 (#36 punto 7)

json.JSONDecodeError e' un ValueError: KeyError/JSONDecodeError sulla
risposta del modello finivano nell'except ValueError del router (pensato
per i nostri errori di configurazione, es. API key mancante) e uscivano
come 400 invece di 502. Nuova AIResponseError dedicata in ai_service.py
per contenuto malformato o JSON non valido; recipes.py la traduce in 502,
toccato solo per questo except.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-09-21 22:43:42 +02:00
parent 65f6a4b18b
commit 403c923188
4 changed files with 127 additions and 2 deletions
+5
View File
@@ -189,6 +189,11 @@ async def parse_technical_sheet(
tasks = await ai_service.parse_technical_sheet(pdf_bytes)
except ValueError as e:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
except ai_service.AIResponseError as e:
# The AI model's own response was malformed (bad JSON, missing
# fields): that is the upstream service's fault, not ours (400).
logging.getLogger(__name__).error("AI response malformed: %s", e)
raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="Risposta del servizio AI non valida")
except Exception as e:
logging.getLogger(__name__).error("AI parsing error: %s", e)
raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="Errore nel servizio AI")