style: format with ruff
This commit is contained in:
1 parent
13bddd43c7
commit
b0863e5ddb
181 files changed
+2237
-5272
No files matched your search
@@ -86,10 +86,7 @@ def _extract_context(exc: Exception) -> dict[str, Any]:
|
||||
|
||||
|
||||
def _error_body(
|
||||
exc: Exception,
|
||||
*,
|
||||
code_override: str | None = None,
|
||||
critical_override: bool | None = None,
|
||||
exc: Exception, *, code_override: str | None = None, critical_override: bool | None = None
|
||||
) -> dict[str, Any]:
|
||||
"""Build the unified error response body for any exception.
|
||||
|
||||
@@ -125,9 +122,7 @@ 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(
|
||||
@@ -159,10 +154,7 @@ def register_exception_handlers(app) -> None:
|
||||
**body["context"],
|
||||
},
|
||||
)
|
||||
return JSONResponse(
|
||||
status_code=api_status.HTTP_400_BAD_REQUEST,
|
||||
content=body,
|
||||
)
|
||||
return JSONResponse(status_code=api_status.HTTP_400_BAD_REQUEST, content=body)
|
||||
|
||||
@app.exception_handler(AareAuthError)
|
||||
async def _handle_auth_error(request: Request, exc: AareAuthError) -> JSONResponse:
|
||||
@@ -189,26 +181,16 @@ def register_exception_handlers(app) -> None:
|
||||
"status_code": status,
|
||||
},
|
||||
)
|
||||
return JSONResponse(
|
||||
status_code=status,
|
||||
content=body,
|
||||
headers=getattr(exc, "headers", None),
|
||||
)
|
||||
return JSONResponse(status_code=status, content=body, headers=getattr(exc, "headers", 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."""
|
||||
detail = exc.detail
|
||||
log = logger.error if exc.status_code >= 500 else logger.warning
|
||||
log(
|
||||
"HTTPException %s: %s",
|
||||
exc.status_code,
|
||||
detail,
|
||||
)
|
||||
log("HTTPException %s: %s", exc.status_code, detail)
|
||||
# Preserve legacy callers that passed a {code, message} dict
|
||||
if isinstance(detail, dict) and "code" in detail and "message" in detail:
|
||||
legacy_code = str(detail.get("code"))
|
||||
@@ -220,9 +202,7 @@ 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,
|
||||
)
|
||||
@@ -245,8 +225,6 @@ def register_exception_handlers(app) -> None:
|
||||
return JSONResponse(
|
||||
status_code=api_status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
content=_error_body(
|
||||
exc,
|
||||
code_override=str(AareErrorCode.INTERNAL_ERROR),
|
||||
critical_override=True,
|
||||
exc, code_override=str(AareErrorCode.INTERNAL_ERROR), critical_override=True
|
||||
),
|
||||
)
|
||||
Reference in new issue
Block a user