Files
mtusi2026lab3/app/errors.py
T
Artu-RitoandClaude Sonnet 5 2f1cf3afac Лаба 3: Nginx LB, 2 реплики API и общая идемпотентность (оценка 4)
Переиспользован стек lab2 (Postgres/Redis/Argon2id). Добавлены: Nginx
балансировщик перед api1/api2, общая идемпотентность POST-сообщений
(idempotency_keys + advisory-lock сериализация в Postgres), explicit
stop-one/restart hooks. make test/test-persistence/test-failover/validate
зелёные для обеих реплик; найдены и исправлены гонка миграций и гонка
рестарта Nginx/DNS — см. REPORT.md.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-30 00:35:53 +03:00

30 lines
823 B
Python

"""Canonical error envelope: {"error": {"code", "message", "request_id"}}."""
class ApiError(Exception):
def __init__(self, status: int, code: str, message: str):
self.status = status
self.code = code
self.message = message
super().__init__(message)
def bad_request(message: str) -> ApiError:
return ApiError(400, "invalid_request", message)
def unauthorized(message: str = "authentication required") -> ApiError:
return ApiError(401, "unauthorized", message)
def not_found(message: str = "not found") -> ApiError:
return ApiError(404, "not_found", message)
def conflict(message: str) -> ApiError:
return ApiError(409, "conflict", message)
def unavailable(message: str = "dependency unavailable") -> ApiError:
return ApiError(503, "unavailable", message)