prepared skeleton for lab5

This commit is contained in:
2026-11-07 16:10:32 +03:00
parent 276dbdad8a
commit 0a70358829
135 changed files with 9647 additions and 34 deletions
View File
+109
View File
@@ -0,0 +1,109 @@
from collections.abc import Mapping
from PIL import Image, UnidentifiedImageError
BASIC_CHALLENGE = 'Basic realm="messenger", charset="UTF-8"'
BEARER_CHALLENGE = 'Bearer realm="messenger"'
class ApiError(Exception):
def __init__(
self,
status_code: int,
code: str,
message: str,
headers: Mapping[str, str] | None = None,
) -> None:
super().__init__(message)
self.status_code = status_code
self.code = code
self.message = message
self.headers = headers
def invalid_request(message: str = "Неверный запрос") -> ApiError:
"""Создаёт ошибку некорректного или недопустимого запроса."""
return ApiError(400, "invalid_request", message)
def unauthorized(
message: str = "Нет действительной аутентификации",
www_authenticate: str | None = None,
) -> ApiError:
"""Создаёт ошибку отсутствующей или недействительной аутентификации."""
headers = {"WWW-Authenticate": www_authenticate} if www_authenticate else None
return ApiError(401, "unauthorized", message, headers)
def unauthorized_basic(message: str = "Неверные учётные данные") -> ApiError:
"""401 для POST /auth/sessions (одинаковый для неверного username и пароля)."""
return unauthorized(message, BASIC_CHALLENGE)
def unauthorized_bearer(message: str = "Нет действительной сессии") -> ApiError:
"""401 для бизнес-endpoint: нет, истёк, отозван или неизвестен токен."""
return unauthorized(message, BEARER_CHALLENGE)
def not_found(message: str = "Объект не найден") -> ApiError:
"""Создаёт ошибку, указывающую на отсутствие запрошенного ресурса."""
return ApiError(404, "not_found", message)
def conflict(message: str = "Конфликт") -> ApiError:
"""Создаёт ошибку конфликта с текущим состоянием ресурса."""
return ApiError(409, "conflict", message)
def payload_too_large(message: str = "Слишком большое тело запроса") -> ApiError:
"""Создаёт ошибку, указывающую на превышение допустимого размера запроса."""
return ApiError(413, "payload_too_large", message)
def unsupported_media_type(
message: str = "Требуется Content-Type: application/json",
) -> ApiError:
"""Создаёт ошибку неподдерживаемого формата или типа содержимого запроса."""
return ApiError(415, "unsupported_media_type", message)
def rate_limited(retry_after: int = 1) -> ApiError:
"""Создаёт ошибку превышения допустимой частоты запросов."""
return ApiError(
429,
"rate_limited",
"Слишком много запросов",
{"Retry-After": str(max(1, retry_after))},
)
def unavailable(message: str = "Сервис временно недоступен") -> ApiError:
"""Создаёт ошибку временной недоступности сервиса."""
return ApiError(503, "unavailable", message)
def internal_error() -> ApiError:
"""Создаёт ошибку внутренней ошибки сервера."""
return ApiError(500, "internal_error", "Внутренняя ошибка сервера")
IMAGE_WORKER_ERRORS = (
ApiError, Image.DecompressionBombError, Image.DecompressionBombWarning,
MemoryError, UnidentifiedImageError, OSError, ValueError, SyntaxError,
)
def image_worker_error_code(error: Exception) -> int:
code = 2
resource_error = isinstance(error, (Image.DecompressionBombError, Image.DecompressionBombWarning, MemoryError))
declared_limit = isinstance(error, ApiError) and error.status_code == 413
if resource_error or declared_limit:
code = 3
return code
def image_validation_error(code: int) -> ApiError:
error = unsupported_media_type("Повреждённое изображение или несовпадение MIME")
if code == 3 or code < 0:
error = payload_too_large("Превышен лимит пикселей или ресурсов изображения")
return error
+225
View File
@@ -0,0 +1,225 @@
import asyncio
import logging
from contextlib import asynccontextmanager
from collections.abc import Mapping
from uuid import uuid4
from fastapi import FastAPI, Request
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse
from minio.error import MinioException
from redis.exceptions import ConnectionError as RedisConnectionError
from redis.exceptions import TimeoutError as RedisTimeoutError
from sqlalchemy.exc import DBAPIError, InterfaceError, OperationalError, SQLAlchemyError
from sqlalchemy.exc import TimeoutError as PoolTimeoutError
from starlette.exceptions import HTTPException as StarletteHTTPException
from starlette.requests import ClientDisconnect
from urllib3.exceptions import HTTPError as StorageHTTPError
from app.core.instance import INSTANCE_ID_HEADER
from app.core.request_id import REQUEST_ID_HEADER
from app.errors.exceptions import ApiError, internal_error, invalid_request, not_found, unavailable
logger = logging.getLogger(__name__)
TRANSIENT_SQLSTATE_CLASSES = ("08", "40", "53", "57")
TRANSIENT_SQLSTATES = frozenset({"55P03"})
RECOVERABLE_STORAGE_ERRORS = (
ApiError, SQLAlchemyError, MinioException, StorageHTTPError, OSError, TimeoutError,
)
@asynccontextmanager
async def operation_timeout(seconds: float, error: ApiError):
"""Единое преобразование таймаутов; владелец операции задаёт ожидаемую ошибку."""
try:
async with asyncio.timeout(seconds):
yield
except TimeoutError:
raise error from None
async def client_disconnect_handler(request: Request, exc: Exception) -> JSONResponse:
return await api_error_handler(request, invalid_request("Загрузка прервана клиентом"))
def log_recoverable_error(operation: str, error: Exception) -> None:
"""Согласованный безопасный лог восстановления без содержимого SDK-ошибки."""
logger.warning('%s deferred: %s', operation, type(error).__name__)
def _get_request_id(request: Request) -> str:
request_id = getattr(request.state, "request_id", None)
if request_id is None:
request_id = str(uuid4())
request.state.request_id = request_id
return request_id
def _build_response(
request: Request,
status_code: int,
code: str,
message: str,
headers: Mapping[str, str] | None = None,
) -> JSONResponse:
request_id = _get_request_id(request)
response_headers = {
**(headers or {}),
REQUEST_ID_HEADER: request_id,
}
instance_id = getattr(request.app.state, "instance_id", None)
if instance_id is not None:
response_headers[INSTANCE_ID_HEADER] = instance_id
body = {
"error": {
"code": code,
"message": message,
"request_id": request_id,
}
}
return JSONResponse(
status_code=status_code,
content=body,
headers=response_headers,
)
async def api_error_handler(
request: Request,
exc: Exception,
) -> JSONResponse:
assert isinstance(exc, ApiError)
return _build_response(
request,
exc.status_code,
exc.code,
exc.message,
exc.headers,
)
async def routing_error_handler(
request: Request,
exc: Exception,
) -> JSONResponse:
assert isinstance(exc, StarletteHTTPException)
error = invalid_request()
if exc.status_code == 404:
error = not_found()
return _build_response(
request,
exc.status_code,
error.code,
error.message,
exc.headers,
)
async def validation_error_handler(
request: Request,
exc: Exception,
) -> JSONResponse:
assert isinstance(exc, RequestValidationError)
fields = sorted({".".join(map(str, error["loc"])) for error in exc.errors()})
message = "Неверный запрос"
if fields:
message = f"{message}: {', '.join(fields)}"
return _build_response(
request,
400,
"invalid_request",
message,
)
async def dependency_unavailable_handler(
request: Request,
exc: Exception,
) -> JSONResponse:
"""PostgreSQL/Redis/S3 недоступны либо исчерпан пул: 503 без внутренних деталей."""
request_id = _get_request_id(request)
logger.error(
"Dependency unavailable request_id=%s: %s",
request_id,
type(exc).__name__,
)
error = unavailable()
return _build_response(
request,
error.status_code,
error.code,
error.message,
{"Retry-After": "1"},
)
def _is_transient_database_error(exc: DBAPIError) -> bool:
sqlstate = getattr(exc.orig, "sqlstate", None) or ""
return (
sqlstate.startswith(TRANSIENT_SQLSTATE_CLASSES)
or sqlstate in TRANSIENT_SQLSTATES
)
async def database_error_handler(
request: Request,
exc: Exception,
) -> JSONResponse:
"""Временные ошибки PostgreSQL (таймаут запроса, блокировки, нехватка ресурсов) — 503."""
assert isinstance(exc, DBAPIError)
handler = unhandled_error_handler
if _is_transient_database_error(exc):
handler = dependency_unavailable_handler
return await handler(request, exc)
async def unhandled_error_handler(
request: Request,
exc: Exception,
) -> JSONResponse:
request_id = _get_request_id(request)
logger.error(
"Unhandled error request_id=%s",
request_id,
exc_info=exc,
)
error = internal_error()
return _build_response(
request,
error.status_code,
error.code,
error.message,
)
def register_exception_handlers(app: FastAPI) -> None:
app.add_exception_handler(ApiError, api_error_handler)
app.add_exception_handler(ClientDisconnect, client_disconnect_handler)
app.add_exception_handler(MinioException, dependency_unavailable_handler)
app.add_exception_handler(StorageHTTPError, dependency_unavailable_handler)
app.add_exception_handler(StarletteHTTPException, routing_error_handler)
app.add_exception_handler(RequestValidationError, validation_error_handler)
app.add_exception_handler(OperationalError, dependency_unavailable_handler)
app.add_exception_handler(InterfaceError, dependency_unavailable_handler)
app.add_exception_handler(PoolTimeoutError, dependency_unavailable_handler)
app.add_exception_handler(DBAPIError, database_error_handler)
app.add_exception_handler(RedisConnectionError, dependency_unavailable_handler)
app.add_exception_handler(RedisTimeoutError, dependency_unavailable_handler)
app.add_exception_handler(OSError, dependency_unavailable_handler)
app.add_exception_handler(Exception, unhandled_error_handler)