Spaces:
Running on Zero
Running on Zero
Download app/core/errors.py from Ashu1007/EmpathVoice: direct link, hf CLI and curl.
- Browser
- Download file 1.77 kB
-
https://huggingface.co/spaces/Ashu1007/EmpathVoice/resolve/main/app/core/errors.py
- Command line
-
hf download hf://spaces/Ashu1007/EmpathVoice/app/core/errors.py
-
curl -L -o errors.py https://huggingface.co/spaces/Ashu1007/EmpathVoice/resolve/main/app/core/errors.py
1.77 kB
| """Application error types and their HTTP translation. | |
| Routes raise these semantic errors; a single set of handlers turns them into | |
| consistent JSON responses ``{"error": {"code", "message"}}`` with the right | |
| status code. This keeps raw tracebacks out of responses and gives the frontend | |
| a stable error shape to render. | |
| """ | |
| from __future__ import annotations | |
| from fastapi import FastAPI, Request | |
| from fastapi.responses import JSONResponse | |
| class AppError(Exception): | |
| """Base class for expected, client-facing errors.""" | |
| status_code: int = 500 | |
| code: str = "internal_error" | |
| def __init__(self, message: str) -> None: | |
| super().__init__(message) | |
| self.message = message | |
| class InvalidRequestError(AppError): | |
| status_code = 400 | |
| code = "invalid_request" | |
| class ModelNotReadyError(AppError): | |
| status_code = 503 | |
| code = "model_not_ready" | |
| class SynthesisError(AppError): | |
| status_code = 502 | |
| code = "synthesis_failed" | |
| class AudioNotFoundError(AppError): | |
| status_code = 404 | |
| code = "audio_not_found" | |
| def _error_response(status_code: int, code: str, message: str) -> JSONResponse: | |
| return JSONResponse( | |
| status_code=status_code, | |
| content={"error": {"code": code, "message": message}}, | |
| ) | |
| def register_exception_handlers(app: FastAPI) -> None: | |
| async def _handle_app_error(_: Request, exc: AppError) -> JSONResponse: | |
| return _error_response(exc.status_code, exc.code, exc.message) | |
| async def _handle_unexpected(_: Request, exc: Exception) -> JSONResponse: | |
| # Last resort: never leak a traceback to the client. | |
| return _error_response(500, "internal_error", "An unexpected error occurred.") | |