diff --git a/app/route/execute/exception/code_visualize_error.py b/app/route/execute/exception/code_visualize_error.py new file mode 100644 index 0000000..2984f95 --- /dev/null +++ b/app/route/execute/exception/code_visualize_error.py @@ -0,0 +1,11 @@ +from starlette.status import HTTP_400_BAD_REQUEST + +from app.web.exception.base_exception import BaseCustomException +from app.web.exception.enum.error_enum import ErrorEnum + + +class CodeVisualizeError(BaseCustomException): + def __init__(self, error_enum: ErrorEnum, result: dict = None): + super().__init__( + status_code=HTTP_400_BAD_REQUEST, error_enum=error_enum, result={} if result is None else result + ) \ No newline at end of file diff --git a/app/route/execute/service/analsys_service.py b/app/route/execute/service/analsys_service.py index a9d8bb0..4387bec 100644 --- a/app/route/execute/service/analsys_service.py +++ b/app/route/execute/service/analsys_service.py @@ -1,12 +1,16 @@ import requests from app.config.settings import Settings +from app.route.execute.exception.code_visualize_error import CodeVisualizeError +from app.web.exception.enum.error_enum import ErrorEnum def analyze_code(source_code: str, user_input: str): visualise_url = "/".join([Settings.ENGINE_SERVER, "v1", "python"]) - success_response = requests.post( + response = requests.post( visualise_url, json={"source_code": source_code, "input": user_input} ) - return success_response.json().get("result").get("code") + if not response.ok: + raise CodeVisualizeError(ErrorEnum.CODE_VISUALIZE_ERROR) + return response.json().get("result").get("code") diff --git a/app/web/exception/enum/error_enum.py b/app/web/exception/enum/error_enum.py index 0231502..bdd988b 100644 --- a/app/web/exception/enum/error_enum.py +++ b/app/web/exception/enum/error_enum.py @@ -11,6 +11,7 @@ class ErrorEnum(Enum): CODE_EXEC_ERROR = "CS-400004", "지원하지 않는 형식입니다." INPUT_SIZE_MATCHING_ERROR = "CS-400005", "사용자 입력 개수가 일치하지 않습니다." + CODE_VISUALIZE_ERROR = "CS-400006", "아직 시각화할 수 없는 문법이 포함되어 있습니다." #500 OPENAI_SERVER_ERROR = ("CS-504001", "Open AI internal server error") diff --git a/app/web/exception_handlers.py b/app/web/exception_handlers.py index c7de411..7f15e5f 100644 --- a/app/web/exception_handlers.py +++ b/app/web/exception_handlers.py @@ -1,4 +1,4 @@ -from typing import Union +from venv import logger from openai import OpenAIError from starlette import status @@ -7,12 +7,12 @@ from app.route.execute.exception.code_execute_error import CodeExecuteError from app.route.execute.exception.code_syntax_error import CodeSyntaxError +from app.route.execute.exception.code_visualize_error import CodeVisualizeError from app.web.exception.base_exception import BaseCustomException from app.web.exception.enum.error_enum import ErrorEnum from app.web.exception.invalid_exception import InvalidException from app.route.advice.exception.openai_exception import OpenaiException from app.web.models.error_response import ErrorResponse -from app.web.logger import logger def setup_exception_handlers(app: FastAPI): @@ -44,7 +44,8 @@ async def openai_exception_handler(request: Request, exc: OpenaiException): @app.exception_handler(CodeExecuteError) @app.exception_handler(CodeSyntaxError) - async def code_error_exception_handler(request: Request, exc: CodeExecuteError | CodeSyntaxError): + @app.exception_handler(CodeVisualizeError) + async def code_error_exception_handler(request: Request, exc: CodeExecuteError | CodeSyntaxError | CodeVisualizeError): 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())