forked from mtusi2026/mtusi2026lab2
- схема и миграции (migrations/0001_init.sql, python -m messenger.migrate); - регистрация с паролем (Argon2id), вход по Basic, opaque Bearer, logout, expiry; - сессии: SESSION_STORE=valkey (AOF, appendfsync always) или postgres; - пул соединений, statement/lock timeout и клиентский дедлайн: отказ БД = 503; - keyset-пагинация по индексу (chat_id, created_at, id), два запроса без OR; - compose.yaml с закреплёнными образами, secrets, restart.sh. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
66 lines
1.7 KiB
Python
66 lines
1.7 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 Unavailable(ApiError):
|
|
status = 503
|
|
code = "unavailable"
|
|
default_message = "Сервис временно недоступен"
|