안녕하세요 예외 처리에 대한 질문입니다. 단순히 다음과 같이 FastAPI를 사용하면 사용...
# ask-for-help
k
안녕하세요 예외 처리에 대한 질문입니다. 단순히 다음과 같이 FastAPI를 사용하면 사용자 예외를 정의하고 이벤트 핸들러를 app에 추가하여 예외 발생시 응답 메시지를 원하는 대로 처리할 수 있다. 또는 사용자 예외가 아니더라고 pydantic 의 유호성 예외도 이벤트 핸들러를 app에 추가하여 예외 발생시 개발자가 원하는 대로 처리할 수 있다. Bentoml 서비스로 개발시 이와 같이 서비스에 이벤트 핸들러를 등록하는 기능은 없는가? 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"}