Download app/api_errors.py from JohnAlbarkaIbrahim/SentimentScope: direct link, hf CLI and curl.
- Browser
- Download file 1.6 kB
-
https://huggingface.co/spaces/JohnAlbarkaIbrahim/SentimentScope/resolve/main/app/api_errors.py
- Command line
-
hf download hf://spaces/JohnAlbarkaIbrahim/SentimentScope/app/api_errors.py
-
curl -L -o api_errors.py https://huggingface.co/spaces/JohnAlbarkaIbrahim/SentimentScope/resolve/main/app/api_errors.py
1.6 kB
| """Safe, structured HTTP errors for embedding requests.""" | |
| from __future__ import annotations | |
| import time | |
| from dataclasses import dataclass | |
| from fastapi.responses import JSONResponse | |
| from .logging import log | |
| from .metrics import metrics | |
| class APIError(Exception): | |
| status_code: int | |
| code: str | |
| event: str | |
| model_alias: str | None = None | |
| batch_size: int = 0 | |
| retry_after: int | None = None | |
| queue_wait_ms: float = 0 | |
| inference_ms: float = 0 | |
| def __post_init__(self) -> None: | |
| Exception.__init__(self, self.code) | |
| def reject_request( | |
| error: APIError, | |
| request_id: str, | |
| client_label: str, | |
| started_at: float, | |
| ) -> JSONResponse: | |
| metrics.inc_request(ok=False) | |
| total_ms = (time.monotonic() - started_at) * 1000 | |
| if error.model_alias is not None: | |
| metrics.observe_request_duration(error.model_alias, total_ms) | |
| if error.status_code == 429: | |
| metrics.inc_rate_limited() | |
| log.warning( | |
| error.event, | |
| request_id=request_id, | |
| client_label=client_label, | |
| model_alias=error.model_alias, | |
| batch_size=error.batch_size, | |
| queue_wait_ms=round(error.queue_wait_ms, 2), | |
| inference_ms=round(error.inference_ms, 2), | |
| total_ms=round(total_ms, 2), | |
| status="error", | |
| error_code=error.code, | |
| ) | |
| headers = ( | |
| {"Retry-After": str(error.retry_after)} | |
| if error.retry_after is not None | |
| else None | |
| ) | |
| return JSONResponse( | |
| status_code=error.status_code, | |
| content={"error": error.code}, | |
| headers=headers, | |
| ) | |