Spaces:
Running on Zero
Running on Zero
Download src/api/errors.py from QUD-Technologies/quranic-universal-aligner: direct link, hf CLI and curl.
- Browser
- Download file 5.25 kB
-
https://huggingface.co/spaces/QUD-Technologies/quranic-universal-aligner/resolve/main/src/api/errors.py
- Command line
-
hf download hf://spaces/QUD-Technologies/quranic-universal-aligner/src/api/errors.py
-
curl -L -o errors.py https://huggingface.co/spaces/QUD-Technologies/quranic-universal-aligner/resolve/main/src/api/errors.py
5.25 kB
| """Typed API errors shared by the /api/v1 routes and the legacy Gradio adapters. | |
| Services raise these; each interface renders them its own way: | |
| * ``/api/v1`` maps ``status`` to the HTTP status code and serialises | |
| ``to_body()`` as the response body. | |
| * The legacy Gradio endpoints return HTTP 200 with ``to_legacy_dict()`` — | |
| the ``{"error": ..., "segments": []}`` shape production clients parse. | |
| Never raise a bare ``Exception`` out of a service: an unmapped error becomes a | |
| 500 with no ``code`` for the caller to branch on. | |
| """ | |
| from __future__ import annotations | |
| # HTTP statuses, named so call sites read as intent rather than magic numbers. | |
| HTTP_BAD_REQUEST = 400 | |
| HTTP_PAYMENT_REQUIRED = 402 | |
| HTTP_FORBIDDEN = 403 | |
| HTTP_NOT_FOUND = 404 | |
| HTTP_UNPROCESSABLE = 422 | |
| HTTP_UNPROCESSABLE_ENTITY = 422 | |
| HTTP_TOO_MANY_REQUESTS = 429 | |
| HTTP_BAD_GATEWAY = 502 | |
| HTTP_SERVICE_UNAVAILABLE = 503 | |
| class ApiError(Exception): | |
| """Base class for every error a service is allowed to raise.""" | |
| status = HTTP_BAD_REQUEST | |
| code = "error" | |
| def __init__(self, message: str, **extra): | |
| super().__init__(message) | |
| self.message = message | |
| # Extra keys are merged into both renderings (e.g. retry_after_s). | |
| self.extra = {k: v for k, v in extra.items() if v is not None} | |
| def to_body(self) -> dict: | |
| """The /api/v1 error body.""" | |
| return {"code": self.code, "message": self.message, "detail": self.extra or None} | |
| def to_legacy_dict(self, audio_id: str | None = None) -> dict: | |
| """The legacy HTTP-200 error shape.""" | |
| body = {"error": self.message, "error_code": self.code, "segments": [], **self.extra} | |
| if audio_id is not None: | |
| body["audio_id"] = audio_id | |
| return body | |
| class ValidationError(ApiError): | |
| """Caller sent a malformed or unsupported parameter.""" | |
| status = HTTP_BAD_REQUEST | |
| code = "invalid_request" | |
| class SessionNotFoundError(ApiError): | |
| """The audio_id is unknown, malformed, or its session has expired.""" | |
| status = HTTP_NOT_FOUND | |
| code = "session_not_found" | |
| def __init__(self, message: str = "Session not found or expired", **extra): | |
| super().__init__(message, **extra) | |
| class BatchNotFoundError(ApiError): | |
| """The native batch handle is unknown, expired, or owned by another caller.""" | |
| status = HTTP_NOT_FOUND | |
| code = "batch_not_found" | |
| def __init__(self, message: str = "Batch not found or expired", **extra): | |
| super().__init__(message, **extra) | |
| class NotFoundError(ApiError): | |
| """The requested catalog entry does not exist.""" | |
| status = HTTP_NOT_FOUND | |
| code = "not_found" | |
| class CatalogUnavailableError(ApiError): | |
| """The recitation catalog or its storage could not be read. | |
| A server-side dependency failure, not a caller mistake — so 503, and the | |
| caller can sensibly retry. | |
| """ | |
| status = HTTP_SERVICE_UNAVAILABLE | |
| code = "catalog_unavailable" | |
| class DownloadFailedError(ApiError): | |
| """The source URL could not be fetched.""" | |
| status = HTTP_BAD_REQUEST | |
| code = "download_failed" | |
| class NoSpeechError(ApiError): | |
| """Segmentation found nothing to align.""" | |
| status = HTTP_UNPROCESSABLE | |
| code = "no_speech_detected" | |
| def __init__(self, message: str = "No speech detected in audio", **extra): | |
| super().__init__(message, **extra) | |
| class AlignmentFailedError(ApiError): | |
| """The pipeline ran but produced no usable alignment.""" | |
| status = HTTP_UNPROCESSABLE | |
| code = "alignment_failed" | |
| class TimingFailedError(ApiError): | |
| """Word timing failed.""" | |
| status = HTTP_BAD_GATEWAY | |
| code = "timing_failed" | |
| class GpuQuotaError(ApiError): | |
| """ZeroGPU quota is exhausted for this caller. | |
| 402 rather than 429: the caller is not being rate-limited for misbehaving, | |
| they have consumed an allowance that only time or an upgrade restores. | |
| """ | |
| status = HTTP_PAYMENT_REQUIRED | |
| code = "gpu_quota_exhausted" | |
| def __init__(self, message: str, *, anonymous: bool = False, reset_time=None): | |
| super().__init__(message, reset_time=reset_time) | |
| self.anonymous = anonymous | |
| self.reset_time = reset_time | |
| if anonymous: | |
| self.code = "gpu_quota_anonymous" | |
| def to_legacy_dict(self, audio_id: str | None = None) -> dict: | |
| # Production clients read `reset_time` at the top level even when null. | |
| body = super().to_legacy_dict(audio_id) | |
| body["reset_time"] = self.reset_time | |
| return body | |
| class GpuUnavailableError(ApiError): | |
| """ZeroGPU could not grant a Space slot after bounded server retries.""" | |
| status = 503 | |
| code = "gpu_temporarily_unavailable" | |
| def __init__(self, message: str = "GPU capacity is temporarily unavailable."): | |
| super().__init__(message, retry_after_s=5) | |
| class CpuRateLimitedError(ApiError): | |
| """The shared CPU path is saturated or the caller exceeded fair use.""" | |
| status = HTTP_TOO_MANY_REQUESTS | |
| code = "cpu_rate_limited" | |
| def __init__(self, message: str, *, code: str | None = None, retry_after_s: int = 30): | |
| super().__init__(message, retry_after_s=retry_after_s) | |
| self.retry_after_s = retry_after_s | |
| if code: | |
| self.code = code | |