Skip to content

Commit

Permalink
♻️ Refactor: 예외 처리 추가 (#19)
Browse files Browse the repository at this point in the history
* Refactor: username으로 정보 조회 불가 예외 발생

Related to: #17

* Refactor: 각종 예외 처리

Related to: #17

* Feat: Error log 추가

Related to: #17
  • Loading branch information
Taejin1221 authored Nov 19, 2024
1 parent 33e8620 commit ee61c58
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
40 changes: 40 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import traceback

from fastapi import FastAPI
from openai import OpenAIError
from starlette.responses import JSONResponse

from ai_model.gpt_model import GPTModel
from config.database import Session
from config.log import get_logger
from entity.request_dto import ChatRequest
from repository.credit_repository import CreditRepository
from repository.member_detail_repository import MemberDetailRepository
Expand All @@ -25,8 +30,43 @@

chat_service = ChatService(gpt_model, work_service, credit_sevice)

logger = get_logger()

app = FastAPI()

@app.exception_handler(ValueError)
async def value_error_handler(request, exc: ValueError):
error_details = traceback.format_exc()
logger.error(f"Stack Trace:\n{error_details}")
logger.error(f"An error occerred: {str(exc)}")

return JSONResponse(
status_code=400,
content={"message": str(exc)}
)

@app.exception_handler(OpenAIError)
async def openai_error_handler(request, exc: OpenAIError):
error_details = traceback.format_exc()
logger.error(f"Stack Trace:\n{error_details}")
logger.error(f"An error occerred: {str(exc)}")

return JSONResponse(
status_code=400,
content={"message": "OpenAI Error입니다. 관리자에게 문의하세요."}
)

@app.exception_handler(Exception)
async def value_error_handler(request, exc: Exception):
error_details = traceback.format_exc()
logger.error(f"Stack Trace:\n{error_details}")
logger.error(f"An error occerred: {str(exc)}")

return JSONResponse(
status_code=400,
content={"message": "예상치 못한 Error입니다. 관리자에게 문의하세요"}
)

@app.post("/ai/chatbot")
async def chat(request: ChatRequest):
"""
Expand Down
3 changes: 3 additions & 0 deletions service/credit_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ def get_credit_balance(self, username: str) -> int:
:param username: 사용자의 이름
:return: 사용자의 엽전 정보
"""
credit_info = self.credit_repository.find_by_username(username)
if not credit_info:
raise ValueError(f"주어진 {username}으로 엽전 정보를 찾을 수 없습니다.")
return self.credit_repository.find_by_username(username).balance
4 changes: 4 additions & 0 deletions service/work_service.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from enum import member

from entity.work import WorkIdList
from model.enum.physical_status import PhysicalStatus
from model.enum.work_main_category import WorkMainCategory
Expand Down Expand Up @@ -52,6 +54,8 @@ def recommend(
) -> WorkIdList:

member_detail: MemberDetail = self.member_detail_repository.find_by_username(username)
if not member_detail:
raise ValueError(f"주어진 {username}으로 유저 상세 정보를 찾을 수 없습니다.")

query_physical_status = physical_status if physical_status is not None else member_detail.physical_status
query_location = location if location else member_detail.location
Expand Down

0 comments on commit ee61c58

Please sign in to comment.