Реализовать лабораторную 4 на оценку 5
Добавить приватное S3-хранилище, вложения, presigned upload, reconciler и доказательства.
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
"""Typed public errors. Internal details never cross the HTTP boundary."""
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
class ApiError(Exception):
|
||||
def __init__(self, status: int, code: str, message: str, headers: dict[str, str] | None = None):
|
||||
super().__init__(message)
|
||||
self.status = status
|
||||
self.code = code
|
||||
self.message = message
|
||||
self.headers = headers or {}
|
||||
|
||||
|
||||
def bad_request(message: str = "Некорректный запрос") -> ApiError:
|
||||
return ApiError(400, "invalid_request", message)
|
||||
|
||||
|
||||
def unauthorized(*, basic: bool = False) -> ApiError:
|
||||
challenge = (
|
||||
'Basic realm="messenger", charset="UTF-8"'
|
||||
if basic
|
||||
else 'Bearer realm="messenger"'
|
||||
)
|
||||
return ApiError(401, "unauthorized", "Требуется аутентификация", {"WWW-Authenticate": 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 = "Неподдерживаемый тип файла") -> ApiError:
|
||||
return ApiError(415, "unsupported_media_type", message)
|
||||
|
||||
|
||||
def unavailable(message: str = "Сервис временно недоступен") -> ApiError:
|
||||
return ApiError(503, "unavailable", message)
|
||||
Reference in New Issue
Block a user