refactor: use aarecommon and remove common
This commit is contained in:
1 parent
8f0a85316d
commit
13bddd43c7
172 files changed
+8267
-8214
No files matched your search
@@ -10,33 +10,33 @@ bare-Exception fallback. Each emits the unified response body from §3:
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
from aarecommon.errors.codes import AareErrorCode, AuthErrorCode
|
||||
from aarecommon.errors.exception_handler import (
|
||||
AareAuthError,
|
||||
AareDBCommunicationError,
|
||||
AareException,
|
||||
AareUserError,
|
||||
AuthenticationException,
|
||||
AutomationError,
|
||||
CriticalTellException,
|
||||
LoopCenteringFailed,
|
||||
ManualMountException,
|
||||
MountingFailed,
|
||||
SampleException,
|
||||
SmargonCommunicationError,
|
||||
TellCommunicationError,
|
||||
UnmountingFailed,
|
||||
UserRightsException,
|
||||
WarningTellException,
|
||||
)
|
||||
from fastapi import FastAPI, HTTPException
|
||||
from starlette.requests import Request
|
||||
from starlette.responses import JSONResponse
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from aare.daq.server_exception_handler import register_exception_handlers
|
||||
from aare.common.error_codes import AareErrorCode, AuthErrorCode
|
||||
from aare.common.exception_handler import (
|
||||
AareException,
|
||||
AutomationError,
|
||||
AareUserError,
|
||||
AareAuthError,
|
||||
MountingFailed,
|
||||
UnmountingFailed,
|
||||
LoopCenteringFailed,
|
||||
TellCommunicationError,
|
||||
CriticalTellException,
|
||||
WarningTellException,
|
||||
SmargonCommunicationError,
|
||||
AareDBCommunicationError,
|
||||
AuthenticationException,
|
||||
UserRightsException,
|
||||
ManualMountException,
|
||||
SampleException,
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -60,6 +60,7 @@ def _body(response: JSONResponse) -> dict:
|
||||
# AutomationError handler -- 503 if critical, 422 otherwise
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_automation_error_not_critical_returns_422(app, mock_request):
|
||||
# MountingFailed has class default critical=False in Phase 1
|
||||
@@ -76,7 +77,9 @@ async def test_automation_error_not_critical_returns_422(app, mock_request):
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_automation_error_critical_via_instance_flag_returns_503(app, mock_request):
|
||||
async def test_automation_error_critical_via_instance_flag_returns_503(
|
||||
app, mock_request
|
||||
):
|
||||
# Instance-level override: critical=True
|
||||
exc = MountingFailed("threshold breached", critical=True)
|
||||
handler = app.exception_handlers[AutomationError]
|
||||
@@ -87,9 +90,12 @@ async def test_automation_error_critical_via_instance_flag_returns_503(app, mock
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_automation_error_context_contains_endpoint_and_operation(app, mock_request):
|
||||
exc = TellCommunicationError("timeout", endpoint="/state", operation="GET",
|
||||
base_url="http://tell:8000")
|
||||
async def test_automation_error_context_contains_endpoint_and_operation(
|
||||
app, mock_request
|
||||
):
|
||||
exc = TellCommunicationError(
|
||||
"timeout", endpoint="/state", operation="GET", base_url="http://tell:8000"
|
||||
)
|
||||
handler = app.exception_handlers[AutomationError]
|
||||
response = await handler(mock_request, exc)
|
||||
body = _body(response)
|
||||
@@ -100,7 +106,9 @@ async def test_automation_error_context_contains_endpoint_and_operation(app, moc
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_automation_error_excludes_critical_and_headers_from_context(app, mock_request):
|
||||
async def test_automation_error_excludes_critical_and_headers_from_context(
|
||||
app, mock_request
|
||||
):
|
||||
# critical kwarg should not appear in context (it has its own field)
|
||||
exc = MountingFailed("x", critical=True)
|
||||
handler = app.exception_handlers[AutomationError]
|
||||
@@ -114,6 +122,7 @@ async def test_automation_error_excludes_critical_and_headers_from_context(app,
|
||||
# AareUserError handler -- always 400
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_user_error_returns_400(app, mock_request):
|
||||
exc = ManualMountException("user must intervene")
|
||||
@@ -140,6 +149,7 @@ async def test_sample_exception_returns_400_as_user_error(app, mock_request):
|
||||
# AareAuthError handler -- 401 / 403, code from instance.code
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_authentication_exception_returns_401_with_auth_code(app, mock_request):
|
||||
exc = AuthenticationException("Bad token", code=AuthErrorCode.INVALID_TOKEN)
|
||||
@@ -166,8 +176,9 @@ async def test_user_rights_exception_returns_403(app, mock_request):
|
||||
@pytest.mark.asyncio
|
||||
async def test_authentication_exception_preserves_explicit_status(app, mock_request):
|
||||
# AuthenticationException can be constructed with a custom status_code
|
||||
exc = AuthenticationException("Forbidden auth path", status_code=403,
|
||||
code=AuthErrorCode.FORBIDDEN)
|
||||
exc = AuthenticationException(
|
||||
"Forbidden auth path", status_code=403, code=AuthErrorCode.FORBIDDEN
|
||||
)
|
||||
handler = app.exception_handlers[AareAuthError]
|
||||
response = await handler(mock_request, exc)
|
||||
assert response.status_code == 403
|
||||
@@ -177,6 +188,7 @@ async def test_authentication_exception_preserves_explicit_status(app, mock_requ
|
||||
# HTTPException handler -- pass-through status, new shape
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_http_exception_handler_string_detail(app, mock_request):
|
||||
exc = HTTPException(status_code=418, detail="I'm a teapot")
|
||||
@@ -192,7 +204,9 @@ async def test_http_exception_handler_string_detail(app, mock_request):
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_http_exception_handler_dict_detail(app, mock_request):
|
||||
exc = HTTPException(status_code=400, detail={"code": "CUSTOM", "message": "Msg", "field": "foo"})
|
||||
exc = HTTPException(
|
||||
status_code=400, detail={"code": "CUSTOM", "message": "Msg", "field": "foo"}
|
||||
)
|
||||
handler = app.exception_handlers[HTTPException]
|
||||
response = await handler(mock_request, exc)
|
||||
assert response.status_code == 400
|
||||
@@ -215,6 +229,7 @@ async def test_http_exception_5xx_is_critical(app, mock_request):
|
||||
# Bare Exception fallback -- 500, critical=True
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_unhandled_exception_is_critical_500(app, mock_request):
|
||||
exc = ValueError("oops")
|
||||
@@ -229,7 +244,9 @@ async def test_unhandled_exception_is_critical_500(app, mock_request):
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_unhandled_exception_empty_message_falls_back_to_class_name(app, mock_request):
|
||||
async def test_unhandled_exception_empty_message_falls_back_to_class_name(
|
||||
app, mock_request
|
||||
):
|
||||
exc = ValueError()
|
||||
handler = app.exception_handlers[Exception]
|
||||
response = await handler(mock_request, exc)
|
||||
@@ -241,6 +258,7 @@ async def test_unhandled_exception_empty_message_falls_back_to_class_name(app, m
|
||||
# Handler count -- the design contract is 4 + HTTPException + Exception
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_only_four_root_handlers_plus_fallbacks(app):
|
||||
"""Plan §2: collapse the ~25 per-class handlers down to 4 roots, plus the
|
||||
HTTPException pass-through and the bare-Exception fallback.
|
||||
@@ -250,24 +268,34 @@ def test_only_four_root_handlers_plus_fallbacks(app):
|
||||
counted toward our handler budget. We assert (a) our expected handlers
|
||||
are present and (b) no per-class AareException handlers remain.
|
||||
"""
|
||||
from aare.common.exception_handler import (
|
||||
MountingFailed, UnmountingFailed, TellCommunicationError,
|
||||
LoopCenteringFailed, CriticalTellException, WarningTellException,
|
||||
SmargonCommunicationError, AareDBCommunicationError,
|
||||
from aarecommon.errors.exception_handler import (
|
||||
AareDBCommunicationError,
|
||||
CriticalTellException,
|
||||
LoopCenteringFailed,
|
||||
MountingFailed,
|
||||
SmargonCommunicationError,
|
||||
TellCommunicationError,
|
||||
UnmountingFailed,
|
||||
WarningTellException,
|
||||
)
|
||||
|
||||
registered = set(app.exception_handlers.keys())
|
||||
expected = {AutomationError, AareUserError, AareAuthError,
|
||||
HTTPException, Exception}
|
||||
expected = {AutomationError, AareUserError, AareAuthError, HTTPException, Exception}
|
||||
assert expected.issubset(registered), (
|
||||
f"Missing required handlers: {expected - registered}"
|
||||
)
|
||||
|
||||
# No leftover per-class handlers from the old design
|
||||
forbidden = {MountingFailed, UnmountingFailed, TellCommunicationError,
|
||||
LoopCenteringFailed, CriticalTellException,
|
||||
WarningTellException, SmargonCommunicationError,
|
||||
AareDBCommunicationError}
|
||||
forbidden = {
|
||||
MountingFailed,
|
||||
UnmountingFailed,
|
||||
TellCommunicationError,
|
||||
LoopCenteringFailed,
|
||||
CriticalTellException,
|
||||
WarningTellException,
|
||||
SmargonCommunicationError,
|
||||
AareDBCommunicationError,
|
||||
}
|
||||
leftover = forbidden & registered
|
||||
assert not leftover, f"Per-class handlers must be removed: {leftover}"
|
||||
|
||||
@@ -290,7 +318,9 @@ async def test_automation_error_logs_error_when_critical(app, mock_request, capl
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_automation_error_logs_warning_when_not_critical(app, mock_request, caplog):
|
||||
async def test_automation_error_logs_warning_when_not_critical(
|
||||
app, mock_request, caplog
|
||||
):
|
||||
exc = MountingFailed("benign")
|
||||
handler = app.exception_handlers[AutomationError]
|
||||
caplog.clear()
|
||||
@@ -315,7 +345,10 @@ async def test_response_body_shape_uniform(app, mock_request):
|
||||
cases: list[tuple[type, Exception]] = [
|
||||
(AutomationError, MountingFailed("x")),
|
||||
(AutomationError, LoopCenteringFailed("y")),
|
||||
(AutomationError, SmargonCommunicationError("z", operation="GET", endpoint="/e")),
|
||||
(
|
||||
AutomationError,
|
||||
SmargonCommunicationError("z", operation="GET", endpoint="/e"),
|
||||
),
|
||||
(AutomationError, AareDBCommunicationError("db", critical=True)),
|
||||
(AareUserError, ManualMountException("m")),
|
||||
(AareUserError, SampleException("s")),
|
||||
|
||||
Reference in new issue
Block a user