Some checks failed
Aegis CI / lint-and-test (push) Has been cancelled
Foundational changes required before any new feature work can begin. - 0.1 Redis infrastructure: add redis:7-alpine to docker-compose dev and prod, REDIS_URL config, singleton client in app/infrastructure/redis_client.py - 0.2 Token blacklist on Redis SEC-001: replace in-memory dict with Redis SETEX keyed by jti, auto-expiring TTL derived from token exp - 0.3 Database indexes SR-006: Alembic migration b019 with 5 composite indexes for scoring, MTTD/MTTR, remediation, and notification queries - 0.4 Domain exceptions TD-003: app/domain/exceptions.py with typed errors, error_handler middleware mapping them to HTTP, services decoupled from FastAPI - 0.5 Fix silenced exceptions TD-007: replace 4 bare except-pass blocks in test_workflow_service with logger.warning with exc_info - 0.6 CI pipeline TD-009: GitHub Actions workflow with Postgres and Redis service containers, ruff lint, pytest; ruff.toml for baseline config
44 lines
1.3 KiB
Python
44 lines
1.3 KiB
Python
"""Domain exception → HTTP response mapping.
|
|
|
|
This module provides a single exception handler that converts
|
|
domain-layer exceptions into structured JSON responses, keeping
|
|
the service layer free from FastAPI's ``HTTPException``.
|
|
"""
|
|
|
|
from fastapi import Request
|
|
from fastapi.responses import JSONResponse
|
|
|
|
from app.domain.exceptions import (
|
|
AuthorizationError,
|
|
DomainException,
|
|
DuplicateEntityError,
|
|
EntityNotFoundError,
|
|
InvalidOperationError,
|
|
InvalidTransitionError,
|
|
)
|
|
|
|
EXCEPTION_STATUS_MAP: dict[type[DomainException], int] = {
|
|
EntityNotFoundError: 404,
|
|
DuplicateEntityError: 409,
|
|
InvalidTransitionError: 400,
|
|
InvalidOperationError: 400,
|
|
AuthorizationError: 403,
|
|
}
|
|
|
|
|
|
async def domain_exception_handler(
|
|
request: Request,
|
|
exc: DomainException,
|
|
) -> JSONResponse:
|
|
"""Convert a :class:`DomainException` into a JSON error response."""
|
|
status_code = EXCEPTION_STATUS_MAP.get(type(exc), 400)
|
|
|
|
content: dict = {"detail": exc.message, "code": exc.code}
|
|
|
|
if isinstance(exc, InvalidTransitionError):
|
|
content["current_state"] = exc.current_state
|
|
content["target_state"] = exc.target_state
|
|
content["valid_transitions"] = exc.valid_transitions
|
|
|
|
return JSONResponse(status_code=status_code, content=content)
|