Skip to content

Commit

Permalink
[#34]feat: 에러 로그 출력
Browse files Browse the repository at this point in the history
  • Loading branch information
ujkkk committed Nov 6, 2024
1 parent 350a64a commit 185a2e4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 deletions.
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
5 changes: 3 additions & 2 deletions app/web/exception/enum/error_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ class ErrorEnum(Enum):
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"
UNKNOWN_ERROR = "CA-400999", "The unexpected error"

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으로 잡힐 때 해당 코드 사용


#500
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"
Expand Down
23 changes: 8 additions & 15 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,6 +49,7 @@ 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=status.HTTP_400_BAD_REQUEST, content=response.to_dict())

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 # 상태 코드
}
)

0 comments on commit 185a2e4

Please sign in to comment.