Skip to content

Commit

Permalink
[#30] refactor: 시각화 예외 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
SongGwanSeok committed Nov 1, 2024
1 parent fd0708f commit fb4ce0d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
11 changes: 11 additions & 0 deletions app/route/execute/exception/code_visualize_error.py
Original file line number Diff line number Diff line change
@@ -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
)
8 changes: 6 additions & 2 deletions app/route/execute/service/analsys_service.py
Original file line number Diff line number Diff line change
@@ -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")
1 change: 1 addition & 0 deletions app/web/exception/enum/error_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
7 changes: 4 additions & 3 deletions app/web/exception_handlers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Union
from venv import logger

from openai import OpenAIError
from starlette import status
Expand All @@ -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):
Expand Down Expand Up @@ -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())

Expand Down

0 comments on commit fb4ce0d

Please sign in to comment.