File size: 602 Bytes
692f0d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
"""JSON error responses for the voice API."""

from __future__ import annotations

from fastapi.responses import JSONResponse

from sonicagent_rt.stt.errors import SpeechError


def error_response(exc: SpeechError, request_id: str | None) -> JSONResponse:
    body: dict[str, object] = {"code": exc.code, "message": exc.message}
    if request_id:
        body["request_id"] = request_id
    headers = {"Cache-Control": "no-store"}
    if exc.status_code in {429, 503}:
        headers["Retry-After"] = "5"
    return JSONResponse(status_code=exc.status_code, content={"error": body}, headers=headers)