72 lines
1.9 KiB
Python
72 lines
1.9 KiB
Python
"""Domain errors mapped 1:1 to the contract error codes (contracts/README.md)."""
|
|
from __future__ import annotations
|
|
|
|
BEARER_CHALLENGE = 'Bearer realm="messenger"'
|
|
BASIC_CHALLENGE = 'Basic realm="messenger", charset="UTF-8"'
|
|
|
|
|
|
class ApiError(Exception):
|
|
status = 500
|
|
code = "internal_error"
|
|
default_message = "Внутренняя ошибка сервера"
|
|
|
|
def __init__(self, message: str | None = None):
|
|
self.message = message or self.default_message
|
|
super().__init__(self.message)
|
|
|
|
@property
|
|
def headers(self) -> dict[str, str]:
|
|
return {}
|
|
|
|
|
|
class InvalidRequest(ApiError):
|
|
status = 400
|
|
code = "invalid_request"
|
|
default_message = "Неверный запрос"
|
|
|
|
|
|
class Unauthorized(ApiError):
|
|
"""401 with the challenge of the scheme the endpoint expects (RFC 7235)."""
|
|
|
|
status = 401
|
|
code = "unauthorized"
|
|
default_message = "Требуется действительная сессия"
|
|
|
|
def __init__(self, message: str | None = None, challenge: str = BEARER_CHALLENGE):
|
|
super().__init__(message)
|
|
self.challenge = challenge
|
|
|
|
@property
|
|
def headers(self) -> dict[str, str]:
|
|
return {"WWW-Authenticate": self.challenge}
|
|
|
|
|
|
class NotFound(ApiError):
|
|
status = 404
|
|
code = "not_found"
|
|
default_message = "Объект не найден"
|
|
|
|
|
|
class Conflict(ApiError):
|
|
status = 409
|
|
code = "conflict"
|
|
default_message = "Конфликт"
|
|
|
|
|
|
class PayloadTooLarge(ApiError):
|
|
status = 413
|
|
code = "payload_too_large"
|
|
default_message = "Тело запроса слишком большое"
|
|
|
|
|
|
class UnsupportedMediaType(ApiError):
|
|
status = 415
|
|
code = "unsupported_media_type"
|
|
default_message = "Неподдерживаемый тип содержимого"
|
|
|
|
|
|
class Unavailable(ApiError):
|
|
status = 503
|
|
code = "unavailable"
|
|
default_message = "Сервис временно недоступен"
|