Spaces:
Sleeping
Sleeping
File size: 5,718 Bytes
0f3460d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | """
Standardized Error Response Models
ν΅μΌλ μλ¬ μλ΅ νμ
λͺ¨λ API μλν¬μΈνΈμμ μΌκ΄λ μλ¬ μλ΅ μ 곡
"""
from typing import Optional, Any, Dict
from fastapi import HTTPException, status
from pydantic import BaseModel
class ErrorDetail(BaseModel):
"""
μλ¬ μμΈ μ 보
Attributes:
code: μλ¬ μ½λ (μ: "TOKEN_001", "VALIDATION_ERROR")
message: μ¬μ©μ μΉνμ μλ¬ λ©μμ§
field: μλ¬κ° λ°μν νλ (μ ν)
details: μΆκ° μ 보 (μ ν)
"""
code: str
message: str
field: Optional[str] = None
details: Optional[Dict[str, Any]] = None
class ErrorResponse(BaseModel):
"""
νμ€ μλ¬ μλ΅
Attributes:
success: νμ False
error: μλ¬ μμΈ μ 보
"""
success: bool = False
error: ErrorDetail
def create_error_response(
code: str,
message: str,
status_code: int = status.HTTP_400_BAD_REQUEST,
field: Optional[str] = None,
details: Optional[Dict[str, Any]] = None
) -> HTTPException:
"""
νμ€ μλ¬ μλ΅ μμ±
Args:
code: μλ¬ μ½λ
message: μλ¬ λ©μμ§
status_code: HTTP μν μ½λ
field: μλ¬ νλ (μ ν)
details: μΆκ° μ 보 (μ ν)
Returns:
HTTPException
"""
return HTTPException(
status_code=status_code,
detail=ErrorResponse(
error=ErrorDetail(
code=code,
message=message,
field=field,
details=details
)
).model_dump()
)
# μ¬μ μ μλ μλ¬ μμ± ν¨μ
def bad_request_error(
message: str,
code: str = "BAD_REQUEST",
field: Optional[str] = None
) -> HTTPException:
"""400 Bad Request μλ¬"""
return create_error_response(
code=code,
message=message,
status_code=status.HTTP_400_BAD_REQUEST,
field=field
)
def unauthorized_error(
message: str = "μΈμ¦μ΄ νμν©λλ€",
code: str = "UNAUTHORIZED"
) -> HTTPException:
"""401 Unauthorized μλ¬"""
return create_error_response(
code=code,
message=message,
status_code=status.HTTP_401_UNAUTHORIZED
)
def forbidden_error(
message: str = "μ κ·Ό κΆνμ΄ μμ΅λλ€",
code: str = "FORBIDDEN"
) -> HTTPException:
"""403 Forbidden μλ¬"""
return create_error_response(
code=code,
message=message,
status_code=status.HTTP_403_FORBIDDEN
)
def not_found_error(
resource: str = "리μμ€",
code: str = "NOT_FOUND"
) -> HTTPException:
"""404 Not Found μλ¬"""
return create_error_response(
code=code,
message=f"{resource}λ₯Ό μ°Ύμ μ μμ΅λλ€",
status_code=status.HTTP_404_NOT_FOUND
)
def conflict_error(
message: str,
code: str = "CONFLICT"
) -> HTTPException:
"""409 Conflict μλ¬"""
return create_error_response(
code=code,
message=message,
status_code=status.HTTP_409_CONFLICT
)
def rate_limit_error(
retry_after: int = 60,
code: str = "RATE_LIMIT_EXCEEDED"
) -> HTTPException:
"""429 Too Many Requests μλ¬"""
return create_error_response(
code=code,
message="μμ² νλλ₯Ό μ΄κ³Όνμ΅λλ€. μ μ ν λ€μ μλν΄μ£ΌμΈμ.",
status_code=status.HTTP_429_TOO_MANY_REQUESTS,
details={"retry_after": retry_after}
)
def internal_server_error(
message: str = "μλ² λ΄λΆ μ€λ₯κ° λ°μνμ΅λλ€",
code: str = "INTERNAL_ERROR",
details: Optional[Dict[str, Any]] = None
) -> HTTPException:
"""500 Internal Server Error"""
return create_error_response(
code=code,
message=message,
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
details=details
)
def service_unavailable_error(
message: str = "μλΉμ€λ₯Ό μΌμμ μΌλ‘ μ¬μ©ν μ μμ΅λλ€",
code: str = "SERVICE_UNAVAILABLE"
) -> HTTPException:
"""503 Service Unavailable μλ¬"""
return create_error_response(
code=code,
message=message,
status_code=status.HTTP_503_SERVICE_UNAVAILABLE
)
# λλ©μΈλ³ μλ¬ μ½λ
class ErrorCodes:
"""μλ¬ μ½λ μμ"""
# μΈμ¦/κΆν
UNAUTHORIZED = "UNAUTHORIZED"
FORBIDDEN = "FORBIDDEN"
TOKEN_EXPIRED = "TOKEN_EXPIRED"
# ν ν° κ΄λ ¨
TOKEN_BALANCE_FAILED = "TOKEN_001"
TOKEN_PRODUCT_NOT_FOUND = "TOKEN_002"
TOKEN_INSUFFICIENT = "TOKEN_003"
TOKEN_CHARGE_FAILED = "TOKEN_004"
TOKEN_BONUS_FAILED = "TOKEN_005"
PAYMENT_RECORD_FAILED = "TOKEN_006"
# μ€ν 리 κ΄λ ¨
STORY_NOT_FOUND = "STORY_001"
STORY_CREATE_FAILED = "STORY_002"
STORY_UPDATE_FAILED = "STORY_003"
# μ½μ€ κ΄λ ¨
COURSE_NOT_FOUND = "COURSE_001"
COURSE_CREATE_FAILED = "COURSE_002"
COURSE_GENERATION_FAILED = "COURSE_003"
# μμ
κ΄λ ¨
LIKE_FAILED = "SOCIAL_001"
REVIEW_CREATE_FAILED = "SOCIAL_002"
REVIEW_DELETE_FAILED = "SOCIAL_003"
# λ°μ΄ν° κ²μ¦
VALIDATION_ERROR = "VALIDATION_ERROR"
INVALID_UUID = "INVALID_UUID"
INVALID_COORDINATES = "INVALID_COORDINATES"
# μΌλ°
NOT_FOUND = "NOT_FOUND"
CONFLICT = "CONFLICT"
BAD_REQUEST = "BAD_REQUEST"
RATE_LIMIT_EXCEEDED = "RATE_LIMIT_EXCEEDED"
INTERNAL_ERROR = "INTERNAL_ERROR"
SERVICE_UNAVAILABLE = "SERVICE_UNAVAILABLE"
|