forked from mtusi2026/mtusi2026lab6
ЛР6
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
"""Ошибки приложения.
|
||||
|
||||
Каждый класс знает свой HTTP-статус и код из контракта, поэтому в api.py
|
||||
достаточно одного обработчика, который превращает любую из них в JSON-ответ.
|
||||
"""
|
||||
|
||||
|
||||
class AppError(Exception):
|
||||
status = 500
|
||||
code = "internal_error"
|
||||
|
||||
def __init__(self, message: str, headers: dict[str, str] | None = None):
|
||||
super().__init__(message)
|
||||
self.message = message
|
||||
self.headers = headers or {}
|
||||
|
||||
|
||||
class InvalidRequest(AppError):
|
||||
status, code = 400, "invalid_request"
|
||||
|
||||
|
||||
class Unauthorized(AppError):
|
||||
"""401. Контракт требует заголовок WWW-Authenticate: какую схему ждёт сервер."""
|
||||
status, code = 401, "unauthorized"
|
||||
|
||||
BASIC = 'Basic realm="messenger", charset="UTF-8"' # только для POST /auth/sessions
|
||||
BEARER = 'Bearer realm="messenger"' # для всех остальных защищённых запросов
|
||||
|
||||
def __init__(self, message: str, challenge: str = BEARER):
|
||||
super().__init__(message, {"WWW-Authenticate": challenge})
|
||||
|
||||
|
||||
class NotFound(AppError):
|
||||
status, code = 404, "not_found"
|
||||
|
||||
|
||||
class Conflict(AppError):
|
||||
status, code = 409, "conflict"
|
||||
|
||||
|
||||
class PayloadTooLarge(AppError):
|
||||
"""413: файл больше MAX_UPLOAD_BYTES или картинка больше MAX_IMAGE_PIXELS (с lab4)."""
|
||||
status, code = 413, "payload_too_large"
|
||||
|
||||
|
||||
class UnsupportedMediaType(AppError):
|
||||
"""415: не PNG/JPEG, повреждённый файл или Content-Type не совпадает с содержимым (с lab4)."""
|
||||
status, code = 415, "unsupported_media_type"
|
||||
|
||||
|
||||
class Unavailable(AppError):
|
||||
"""503: нужная зависимость (PostgreSQL) недоступна. Клиент может повторить позже."""
|
||||
status, code = 503, "unavailable"
|
||||
Reference in New Issue
Block a user