ken
10/27/2024, 3:39 AMapp to handle them with specific response messages. Even for Pydantic validation exceptions, you can add event handlers to customize responses. Is there a similar feature in BentoML services where you can register event handlers in the service to handle exceptions this way?
============================================================================
from fastapi import FastAPI, HTTPException, Request
from fastapi.responses import JSONResponse
class CustomException(HTTPException):
def __init__(self, detail: str = "Custom Request failed"):
super().__init__(status_code=401, detail=detail)
app = FastAPI()
@app.exception_handler(CustomException)
async def auth_exception_handler(request: Request, exc: CustomException):
return JSONResponse(
status_code=exc.status_code,
content={"message": exc.detail},
)
@app.get("/protected-route")
async def protected_route(token: str):
if token != "valid-token":
raise CustomException()
return {"message": "You have access to this route"}
===============================================================================