Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#34]feat: 에러코드 명세 및 에러 메시지 추가 #35

Merged
merged 3 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
# 핸들러 등록
exception_handlers.setup_exception_handlers(app)


@app.get("/edupi-assist/health-check", response_class=JSONResponse)
def root():
return JSONResponse(
Expand Down
2 changes: 2 additions & 0 deletions app/route/execute/service/execute_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand All @@ -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))


Expand Down
23 changes: 12 additions & 11 deletions app/web/exception/enum/error_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
25 changes: 9 additions & 16 deletions app/web/exception_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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):
Expand All @@ -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,
Expand All @@ -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 # 상태 코드
}
)
Loading