Exceptions: added handling for tellcommunication and smargon communication errors

This commit is contained in:
2026-02-27 16:24:21 +01:00
parent 02f2eead57
commit c54d3c1f1e
2 changed files with 105 additions and 3 deletions
+31 -2
View File
@@ -16,7 +16,8 @@ from aare.common.exception_handler import (
BeamlineBusyException,
AuthenticationException,
SampleException,
UserRightsException
UserRightsException,
SmargonCommunicationError, TellCommunicationError
)
logger = setup_logger("aareDAQ")
@@ -116,10 +117,38 @@ def register_exception_handlers(app) -> None:
content=_error_payload(code="SAMPLE_NOT_FOUND", message=str(exc) or "Sample not found"),
)
@app.exception_handler(SmargonCommunicationError)
async def smargon_comm_handler(request: Request, exc: SmargonCommunicationError) -> JSONResponse:
return JSONResponse(
status_code=api_status.HTTP_503_SERVICE_UNAVAILABLE,
content=_error_payload(
code="SMARGON_UNAVAILABLE",
message=str(exc) or "Smargon is unavailable",
extra={
"operation": getattr(exc, "operation", None),
"endpoint": getattr(exc, "endpoint", None),
},
),
)
@app.exception_handler(TellCommunicationError)
async def tell_comm_handler(request: Request, exc: TellCommunicationError) -> JSONResponse:
return JSONResponse(
status_code=api_status.HTTP_503_SERVICE_UNAVAILABLE,
content=_error_payload(
code="TELL_UNAVAILABLE",
message=str(exc) or "TELL is unavailable",
extra={
"operation": getattr(exc, "operation", None),
"endpoint": getattr(exc, "endpoint", None),
},
),
)
@app.exception_handler(Exception)
async def unhandled_exception_handler(request: Request, exc: Exception) -> JSONResponse:
logger.exception("Unhandled server exception")
return JSONResponse(
status_code=api_status.HTTP_500_INTERNAL_SERVER_ERROR,
content=_error_payload(code="INTERNAL_SERVER_ERROR", message="Internal server error"),
content=_error_payload(code="INTERNAL_SERVER_ERROR", message=str(exc) or "Internal server error"),
)