Spaces:
Running
Running
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 | |
} | |
) | |