Spaces:
Runtime error
Runtime error
Download utils/error_handlers.py from Multimedika/Bot_Development: direct link, hf CLI and curl.
- Browser
- Download file 1.37 kB
-
https://huggingface.co/spaces/Multimedika/Bot_Development/resolve/main/utils/error_handlers.py
- Command line
-
hf download hf://spaces/Multimedika/Bot_Development/utils/error_handlers.py
-
curl -L -o error_handlers.py https://huggingface.co/spaces/Multimedika/Bot_Development/resolve/main/utils/error_handlers.py
1.37 kB
| from fastapi.responses import JSONResponse | |
| from starlette.status import HTTP_404_NOT_FOUND, HTTP_500_INTERNAL_SERVER_ERROR | |
| from typing import Any | |
| def handle_exception(e: Exception): | |
| """Handles unexpected exceptions with a standard 500 error response.""" | |
| return JSONResponse( | |
| status_code=HTTP_500_INTERNAL_SERVER_ERROR, | |
| content={ | |
| "error": "Internal Server Error", | |
| "message": "An unexpected error occurred.", | |
| "details": str(e) | |
| } | |
| ) | |
| def handle_error(e: Exception, message: str, status_code: int = HTTP_500_INTERNAL_SERVER_ERROR): | |
| """Generic error handler with custom message and status code.""" | |
| return JSONResponse( | |
| status_code=status_code, | |
| content={ | |
| "error": message, | |
| "details": str(e) | |
| } | |
| ) | |
| def not_found_error(message: str): | |
| """Handles 404 errors for not-found cases with a custom message.""" | |
| return JSONResponse( | |
| status_code=HTTP_404_NOT_FOUND, | |
| content={ | |
| "error": "Not Found", | |
| "message": message | |
| } | |
| ) | |
| def no_entries_found(message: str): | |
| """Handles 404 errors for cases where no entries are found.""" | |
| return JSONResponse( | |
| status_code=HTTP_404_NOT_FOUND, | |
| content={ | |
| "error": "No Entries Found", | |
| "message": message | |
| } | |
| ) | |