diff --git a/app/main.py b/app/main.py index 870286a..d235c45 100644 --- a/app/main.py +++ b/app/main.py @@ -36,6 +36,7 @@ # 핸들러 등록 exception_handlers.setup_exception_handlers(app) + @app.get("/edupi-assist/health-check", response_class=JSONResponse) def root(): return JSONResponse( diff --git a/app/route/execute/service/execute_service.py b/app/route/execute/service/execute_service.py index 4c7144c..edfa33c 100644 --- a/app/route/execute/service/execute_service.py +++ b/app/route/execute/service/execute_service.py @@ -5,6 +5,7 @@ from app.route.execute.exception.code_syntax_error import CodeSyntaxError from app.web.exception.enum.error_enum import ErrorEnum from app.web.exception.task_fail_exception import TaskFailException +from app.web.logger import logger FORBIDDEN_IMPORTS = ["os", "sys", "subprocess", "shutil"] @@ -30,6 +31,7 @@ def execute_code(source_code: str, user_input: str): raise CodeSyntaxError(ErrorEnum.CODE_SYNTAX_ERROR, {"error": e.stderr}) except Exception as e: + logger.error("[Unexpected Exception] execute_code()") raise TaskFailException(ErrorEnum.CODE_EXEC_SERVER_ERROR, dict(e.args)) diff --git a/app/web/exception/enum/error_enum.py b/app/web/exception/enum/error_enum.py index b76b9ab..b5c069f 100644 --- a/app/web/exception/enum/error_enum.py +++ b/app/web/exception/enum/error_enum.py @@ -4,20 +4,21 @@ class ErrorEnum(Enum): # 400 - TASK_FAIL = "CS-400001", "There is a problem with the service login" - CODE_SYNTAX_ERROR = "CS-400002", "The code is incorrect syntax" - CODE_CORRECT_FAIL = "CS-400003", "code correct fail" - UNKNOWN_ERROR = "CS-400999", "The unexpected error" + TASK_FAIL = "CA-400001", "There is a problem with the service login" + CODE_SYNTAX_ERROR = "CA-400002", "The code is incorrect syntax" + CODE_CORRECT_FAIL = "CA-400003", "code correct fail" + CODE_EXEC_ERROR = "CA-400004", "The format is not supported for security reasons" + CODE_EXEC_SECURITY_ERROR = "CA-400004", "The format is not supported for security reasons" + INPUT_SIZE_MATCHING_ERROR = "CA-400005", "The number of user inputs does not match." + CODE_VISUALIZE_ERROR = "CA-400006", "It contains syntax that we can't visualize yet." + + UNKNOWN_ERROR = "CA-400999", "The unexpected error" # Exception으로 잡힐 때 해당 코드 사용 - CODE_EXEC_ERROR = "CS-400004", "The format is not supported for security reasons" - CODE_EXEC_SECURITY_ERROR = "CS-400004", "The format is not supported for security reasons" - INPUT_SIZE_MATCHING_ERROR = "CS-400005", "The number of user inputs does not match." - CODE_VISUALIZE_ERROR = "CS-400006", "It contains syntax that we can't visualize yet." #500 - CODE_EXEC_SERVER_ERROR = "CS-503001", "There is a problem with the execute service login" - OPENAI_SERVER_ERROR = "CS-504001", "Open AI internal server error" - OPENAI_MAX_TOKEN_LIMIT = "CS-504002", "Open AI max token limit" + CODE_EXEC_SERVER_ERROR = "CA-503001", "There is a problem with the execute service login" + OPENAI_SERVER_ERROR = "CA-504001", "Open AI internal server error" + OPENAI_MAX_TOKEN_LIMIT = "CA-504002", "Open AI max token limit" def __init__(self, code, detail): diff --git a/app/web/exception_handlers.py b/app/web/exception_handlers.py index a94a763..3371e68 100644 --- a/app/web/exception_handlers.py +++ b/app/web/exception_handlers.py @@ -19,6 +19,8 @@ def setup_exception_handlers(app: FastAPI): @app.exception_handler(InvalidException) async def invalid_exception_handler(request: Request, exc: InvalidException): + logger.info(f"[{exc.error_enum}] : code:{exc.error_enum.code}, detail:{exc.error_enum.detail}, {exc.args}") + response = ErrorResponse( code=exc.error_enum.code, detail=exc.error_enum.detail, @@ -32,12 +34,11 @@ async def invalid_exception_handler(request: Request, exc: InvalidException): @app.exception_handler(OpenaiException) @app.exception_handler(TaskFailException) async def openai_exception_handler(request: Request, exc: OpenaiException | TaskFailException): - logger.info( - f"{exc.error_enum.code} - {exc.error_enum.detail}\n") + logger.error(f"[Task fail Exception] : code:{exc.error_enum.code}, detail:{exc.error_enum.detail}, {exc.args}") response = ErrorResponse( - code=ErrorEnum.TASK_FAIL.code, - detail=ErrorEnum.TASK_FAIL.detail, + code=ErrorEnum.UNKNOWN_ERROR.code, + detail=ErrorEnum.UNKNOWN_ERROR.detail, ) return JSONResponse( status_code=status.HTTP_400_BAD_REQUEST, @@ -48,8 +49,9 @@ async def openai_exception_handler(request: Request, exc: OpenaiException | Task @app.exception_handler(CodeSyntaxError) @app.exception_handler(CodeVisualizeError) async def code_error_exception_handler(request: Request, exc: CodeExecuteError | CodeSyntaxError | CodeVisualizeError): + logger.info(f"[{exc.error_enum}] : code:{exc.error_enum.code}, detail:{exc.error_enum.detail}, {exc.args}") response = ErrorResponse(code=exc.error_enum.code, detail=exc.error_enum.detail, result=exc.result) - return JSONResponse(status_code=exc.status, content=response.to_dict()) + return JSONResponse(status_code=status.HTTP_400_BAD_REQUEST, content=response.to_dict()) @app.exception_handler(BaseCustomException) async def base_exception_handler(request: Request, exc: BaseCustomException): @@ -65,6 +67,7 @@ async def base_exception_handler(request: Request, exc: BaseCustomException): @app.exception_handler(Exception) async def exception_handler(request: Request, exc: Exception): + logger.error(f"[Unknown Exception] : {exc.args}") response = ErrorResponse( code=ErrorEnum.UNKNOWN_ERROR.code, detail=ErrorEnum.UNKNOWN_ERROR.detail, @@ -74,14 +77,4 @@ async def exception_handler(request: Request, exc: Exception): return JSONResponse( status_code=status.HTTP_400_BAD_REQUEST, content=response.to_dict() - ) - - def _get_unique_value_in_openAI_error(e: OpenAIError) -> dict: - response = getattr(e, 'response', None) # response가 없으면 None 반환 - status_code = response.status_code if response else "Unknown" # response가 없으면 'Unknown' - - return { - "error_type": type(e).__name__, # 예외 클래스 이름 - "message": str(e), # 예외 메시지 - "status_code": status_code # 상태 코드 - } \ No newline at end of file + ) \ No newline at end of file