o2o-castad-backend/app/p2v/exceptions.py
2026-08-26 13:52:18 +09:00

115 lines
3.5 KiB
Python

# -*- coding: utf-8 -*-
"""P2V 모듈 도메인 예외.
SsulboxException 과 같은 (message, status_code, code) 형태다.
`code` 는 프론트엔드가 분기에 쓰므로 값을 바꾸면 안 된다.
전역 핸들러 등록: app/core/exceptions.py add_exception_handlers().
"""
from fastapi import status
class P2vException(Exception):
"""P2V 기본 예외"""
def __init__(
self,
message: str,
status_code: int = status.HTTP_500_INTERNAL_SERVER_ERROR,
code: str = "P2V_ERROR",
):
self.message = message
self.status_code = status_code
self.code = code
super().__init__(self.message)
# =============================================================================
# 기능/연결 상태
# =============================================================================
class P2vDisabledError(P2vException):
"""P2V 기능이 설정으로 꺼져 있음"""
def __init__(self):
super().__init__(
message="포스터 생성 기능이 현재 비활성화되어 있습니다.",
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
code="P2V_DISABLED",
)
class P2vUnavailableError(P2vException):
"""P2V 서버 연결 실패 (다운, 타임아웃, 접근 키 불일치)"""
def __init__(self, detail: str = ""):
message = "포스터 생성 서버에 연결할 수 없습니다."
if detail:
message += f" ({detail})"
super().__init__(
message=message,
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
code="P2V_UNAVAILABLE",
)
class P2vUpstreamError(P2vException):
"""P2V 서버가 4xx/5xx 를 돌려줌 — detail 과 상태 코드를 그대로 중계한다"""
def __init__(self, message: str, status_code: int):
super().__init__(
message=message,
status_code=status_code,
code="P2V_UPSTREAM_ERROR",
)
# =============================================================================
# 잡 관련
# =============================================================================
class P2vJobNotFoundError(P2vException):
"""잡을 찾을 수 없음 (없거나 남의 것 — 존재 여부를 노출하지 않는다)"""
def __init__(self):
super().__init__(
message="생성 요청을 찾을 수 없습니다.",
status_code=status.HTTP_404_NOT_FOUND,
code="P2V_JOB_NOT_FOUND",
)
class P2vInvalidStateError(P2vException):
"""현재 상태에서 허용되지 않는 조작 (예: 실패하지 않은 잡의 재시도)"""
def __init__(self, detail: str):
super().__init__(
message=detail,
status_code=status.HTTP_409_CONFLICT,
code="P2V_INVALID_STATE",
)
class P2vFileNotFoundError(P2vException):
"""프록시 화이트리스트 밖이거나 존재하지 않는 파일"""
def __init__(self):
super().__init__(
message="파일을 찾을 수 없습니다.",
status_code=status.HTTP_404_NOT_FOUND,
code="P2V_FILE_NOT_FOUND",
)
class P2vUploadTooLargeError(P2vException):
"""업로드 파일이 상한을 초과"""
def __init__(self, limit_bytes: int):
super().__init__(
message=f"{limit_bytes // (1024 * 1024)}MB 이하만 업로드할 수 있습니다.",
status_code=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE,
code="P2V_UPLOAD_TOO_LARGE",
)