Spaces:
Runtime error
Runtime error
Download backend/core/errors.py from Amitkasar/ai-api-testing: direct link, hf CLI and curl.
- Browser
- Download file 668 Bytes
-
https://huggingface.co/spaces/Amitkasar/ai-api-testing/resolve/main/backend/core/errors.py
- Command line
-
hf download hf://spaces/Amitkasar/ai-api-testing/backend/core/errors.py
-
curl -L -o errors.py https://huggingface.co/spaces/Amitkasar/ai-api-testing/resolve/main/backend/core/errors.py
668 Bytes
| from fastapi import Request | |
| from fastapi.responses import JSONResponse | |
| class AppError(Exception): | |
| """Raise this anywhere in route/service code for a clean, consistent error response.""" | |
| def __init__(self, message: str, code: str = "ERROR", status: int = 400, detail: dict = None): | |
| self.message = message | |
| self.code = code | |
| self.status = status | |
| self.detail = detail or {} | |
| super().__init__(message) | |
| async def app_error_handler(request: Request, exc: AppError) -> JSONResponse: | |
| return JSONResponse( | |
| status_code=exc.status, | |
| content={"error": exc.message, "code": exc.code, "detail": exc.detail}, | |
| ) | |