cleaned version

This commit is contained in:
JiriUhlir
2026-07-10 09:54:05 +02:00
parent 7a3f61c125
commit 5d2ba407f4
7 changed files with 104 additions and 180 deletions
+32 -1
View File
@@ -35,12 +35,43 @@ class BadRequestError(TranscriptionError):
status_code = 400
class RateLimitError(TranscriptionError):
"""Upstream rate limit (HTTP 429)."""
status_code = 429
class UpstreamError(TranscriptionError):
"""Chyba při volání upstream API (Deepgram / OpenAI / stažení audia)."""
"""Chyba při volání upstream API (Deepgram / OpenAI)."""
status_code = 502
def raise_for_upstream(upstream: str, status: int, body: str) -> None:
"""Zmapuje HTTP status z upstreamu (Deepgram/OpenAI) na správnou chybu služby.
- 400/415/422 → 400 (špatný / nepodporovaný audio soubor = chyba klienta)
- 401/403 → 401 (upstream odmítl API klíč volajícího)
- 429 → 429 (rate limit)
- jinak → 502 (výpadek / neočekávaná chyba upstreamu)
"""
snippet = (body or "")[:500]
if status in (400, 415, 422):
raise BadRequestError(
f"{upstream} odmítl audio (HTTP {status}) — pravděpodobně nepodporovaný "
f"formát nebo poškozený soubor.",
detail=snippet,
)
if status in (401, 403):
raise CredentialsError(
f"{upstream} odmítl API klíč (HTTP {status}).",
detail=snippet,
)
if status == 429:
raise RateLimitError(f"{upstream} rate limit (HTTP 429).", detail=snippet)
raise UpstreamError(f"{upstream} vrátil chybu {status}.", detail=snippet)
def register_exception_handlers(app: FastAPI) -> None:
@app.exception_handler(TranscriptionError)
async def _handle_transcription_error(request: Request, exc: TranscriptionError):