Files
mtusi2026lab3/src/messenger/errors.py
T
2026-10-21 18:25:07 +03:00

30 lines
1011 B
Python

"""Ошибки, которые HTTP-слой превращает в ответы контракта."""
class ApiError(Exception):
"""Ответ с ошибкой в формате {"error": {...}}; challenge — значение WWW-Authenticate для 401."""
def __init__(self, status, code, message, challenge=None):
super().__init__(message)
self.status = status
self.code = code
self.message = message
self.challenge = challenge
class Unavailable(Exception):
"""Хранилище (postgres или valkey) не ответило в срок; клиент получит 503."""
def __init__(self, dependency):
super().__init__(dependency)
self.dependency = dependency
def bad_request(message):
return ApiError(400, 'invalid_request', message)
def not_found():
# Чужой объект неотличим от несуществующего.
return ApiError(404, 'not_found', 'Объект не найден')