refactor: use aarecommon and remove common

This commit is contained in:
perl_d committed 2026-07-06 11:53:49 +02:00
1 parent 8f0a85316d
commit 13bddd43c7
172 files changed
+8267 -8214

No files matched your search

+36 -24
View File
@@ -23,40 +23,42 @@ All handlers produce the unified response body defined in §3:
"context": {"endpoint": "/state", "operation": "GET"}
}
"""
from __future__ import annotations
import json
from typing import Any
from aarecommon.config.logger import setup_logger
from aarecommon.errors.codes import AareErrorCode, code_for_exception_class
from aarecommon.errors.exception_handler import (
AareAuthError,
AareException,
AareUserError,
AuthenticationException,
AutomationError,
)
from fastapi import HTTPException
from fastapi import status as api_status
from starlette.requests import Request
from starlette.responses import JSONResponse
from aare.common.logger_config import setup_logger
from aare.common.error_codes import AareErrorCode, code_for_exception_class
from aare.common.exception_handler import (
AareException,
AutomationError,
AareUserError,
AareAuthError,
AuthenticationException,
)
logger = setup_logger("aareDAQ")
# Attributes that live on exceptions for routing/transport, NOT for the
# context payload.
_NON_CONTEXT_ATTRS = frozenset({
"args",
"message",
"critical",
"code",
"headers",
"status_code",
"exception", # BECCommunicationError stores a Python exception here -- not JSON-safe
})
_NON_CONTEXT_ATTRS = frozenset(
{
"args",
"message",
"critical",
"code",
"headers",
"status_code",
"exception", # BECCommunicationError stores a Python exception here -- not JSON-safe
}
)
def _extract_context(exc: Exception) -> dict[str, Any]:
@@ -83,8 +85,12 @@ def _extract_context(exc: Exception) -> dict[str, Any]:
return ctx
def _error_body(exc: Exception, *, code_override: str | None = None,
critical_override: bool | None = None) -> dict[str, Any]:
def _error_body(
exc: Exception,
*,
code_override: str | None = None,
critical_override: bool | None = None,
) -> dict[str, Any]:
"""Build the unified error response body for any exception.
``code_override`` is used by the auth handler to surface the finer-grained
@@ -119,7 +125,9 @@ def register_exception_handlers(app) -> None:
"""
@app.exception_handler(AutomationError)
async def _handle_automation_error(request: Request, exc: AutomationError) -> JSONResponse:
async def _handle_automation_error(
request: Request, exc: AutomationError
) -> JSONResponse:
body = _error_body(exc)
log = logger.error if exc.critical else logger.warning
log(
@@ -188,7 +196,9 @@ def register_exception_handlers(app) -> None:
)
@app.exception_handler(HTTPException)
async def _handle_http_exception(request: Request, exc: HTTPException) -> JSONResponse:
async def _handle_http_exception(
request: Request, exc: HTTPException
) -> JSONResponse:
"""HTTPException isn't in the AareException hierarchy but FastAPI raises
it internally (e.g. validation, route-not-found) and routes raise it
manually. Adapt to the new body shape so clients see one schema."""
@@ -210,7 +220,9 @@ def register_exception_handlers(app) -> None:
"code": legacy_code,
"exception_class": "HTTPException",
"message": legacy_message,
"context": {k: v for k, v in detail.items() if k not in ("code", "message")},
"context": {
k: v for k, v in detail.items() if k not in ("code", "message")
},
},
headers=exc.headers,
)