forked from mtusi2026/mtusi2026lab3
29 lines
781 B
Python
29 lines
781 B
Python
"""Error model shared by the messenger API layers."""
|
|
|
|
|
|
class APIError(Exception):
|
|
"""Structured application error; rendered as ``{"error": {...}}``."""
|
|
|
|
def __init__(self, status_code: int, code: str, message: str, headers: dict | None = None):
|
|
self.status_code = status_code
|
|
self.code = code
|
|
self.message = message
|
|
self.headers = dict(headers or {})
|
|
super().__init__(message)
|
|
|
|
|
|
_ERROR_STATUS: dict[str, int] = {
|
|
"invalid_request": 400,
|
|
"unauthorized": 401,
|
|
"not_found": 404,
|
|
"conflict": 409,
|
|
"payload_too_large": 413,
|
|
"unsupported_media_type": 415,
|
|
"rate_limited": 429,
|
|
"unavailable": 503,
|
|
"internal_error": 500,
|
|
}
|
|
|
|
|
|
def status_for_code(code: str) -> int:
|
|
return _ERROR_STATUS.get(code, 500) |