ЛР4 БВТ2304 Запорожец на 3
Вложения: приватный S3-bucket (RustFS 1.0.4) + метаданные в PostgreSQL, upload PNG/JPEG с лимитами до декодирования, авторизованный download, attachment_ids в сообщениях, идемпотентный bootstrap bucket, failover/persistence/S3-down тесты и REPORT.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
"""Единый формат ошибок {"error":{"code","message","request_id"}}."""
|
||||
from contextvars import ContextVar
|
||||
|
||||
from fastapi.responses import JSONResponse
|
||||
|
||||
request_id_var: ContextVar[str] = ContextVar('request_id', default='-')
|
||||
|
||||
BASIC_CHALLENGE = 'Basic realm="messenger", charset="UTF-8"'
|
||||
BEARER_CHALLENGE = 'Bearer realm="messenger"'
|
||||
|
||||
|
||||
class ApiError(Exception):
|
||||
def __init__(self, status: int, code: str, message: str, headers: dict | None = None):
|
||||
super().__init__(message)
|
||||
self.status = status
|
||||
self.code = code
|
||||
self.message = message
|
||||
self.headers = headers or {}
|
||||
|
||||
|
||||
def invalid(message: str = 'Некорректный запрос') -> ApiError:
|
||||
return ApiError(400, 'invalid_request', message)
|
||||
|
||||
|
||||
def not_found() -> ApiError:
|
||||
# Одинаковый ответ для "нет такого" и "чужое": существование чужого чата не раскрывается.
|
||||
return ApiError(404, 'not_found', 'Объект не найден')
|
||||
|
||||
|
||||
def unauthorized_bearer() -> ApiError:
|
||||
return ApiError(401, 'unauthorized', 'Требуется действительная сессия', {'WWW-Authenticate': BEARER_CHALLENGE})
|
||||
|
||||
|
||||
def unauthorized_basic() -> ApiError:
|
||||
return ApiError(401, 'unauthorized', 'Неверные учётные данные', {'WWW-Authenticate': BASIC_CHALLENGE})
|
||||
|
||||
|
||||
def unavailable() -> ApiError:
|
||||
return ApiError(503, 'unavailable', 'Сервис временно недоступен')
|
||||
|
||||
|
||||
def error_response(status: int, code: str, message: str, headers: dict | None = None) -> JSONResponse:
|
||||
rid = request_id_var.get()
|
||||
return JSONResponse(
|
||||
status_code=status,
|
||||
content={'error': {'code': code, 'message': message, 'request_id': rid}},
|
||||
headers=headers,
|
||||
)
|
||||
Reference in New Issue
Block a user