From ee61c583957e1e26a95b3bf228f888e9a84c1f7b Mon Sep 17 00:00:00 2001 From: wrathlion Date: Tue, 19 Nov 2024 19:32:34 +0900 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Refactor:=20=EC=98=88?= =?UTF-8?q?=EC=99=B8=20=EC=B2=98=EB=A6=AC=20=EC=B6=94=EA=B0=80=20(#19)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Refactor: username으로 정보 조회 불가 예외 발생 Related to: #17 * Refactor: 각종 예외 처리 Related to: #17 * Feat: Error log 추가 Related to: #17 --- main.py | 40 +++++++++++++++++++++++++++++++++++++++ service/credit_service.py | 3 +++ service/work_service.py | 4 ++++ 3 files changed, 47 insertions(+) diff --git a/main.py b/main.py index 31107ce..86e3587 100644 --- a/main.py +++ b/main.py @@ -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 @@ -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): """ diff --git a/service/credit_service.py b/service/credit_service.py index eca688c..9b5a985 100644 --- a/service/credit_service.py +++ b/service/credit_service.py @@ -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 diff --git a/service/work_service.py b/service/work_service.py index a9fb6f5..0e09200 100644 --- a/service/work_service.py +++ b/service/work_service.py @@ -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 @@ -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