Multimedika/Bot_Development
0
1from fastapi.responses import JSONResponse2from starlette.status import HTTP_404_NOT_FOUND, HTTP_500_INTERNAL_SERVER_ERROR3from typing import Any4 5def handle_exception(e: Exception):6 """Handles unexpected exceptions with a standard 500 error response."""7 return JSONResponse(8 status_code=HTTP_500_INTERNAL_SERVER_ERROR,9 content={10 "error": "Internal Server Error",11 "message": "An unexpected error occurred.",12 "details": str(e)13 }14 )15 16def handle_error(e: Exception, message: str, status_code: int = HTTP_500_INTERNAL_SERVER_ERROR):17 """Generic error handler with custom message and status code."""18 return JSONResponse(19 status_code=status_code,20 content={21 "error": message,22 "details": str(e)23 }24 )25 26def not_found_error(message: str):27 """Handles 404 errors for not-found cases with a custom message."""28 return JSONResponse(29 status_code=HTTP_404_NOT_FOUND,30 content={31 "error": "Not Found",32 "message": message33 }34 )35 36def no_entries_found(message: str):37 """Handles 404 errors for cases where no entries are found."""38 return JSONResponse(39 status_code=HTTP_404_NOT_FOUND,40 content={41 "error": "No Entries Found",42 "message": message43 }44 )45 