refactor(vector-sync): dedupe "Bad request" 400s via a helper (SonarCloud S1192)

Extract _bad_request() so the five 400 branches don't duplicate the literal.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Chris Coutinho
2026-06-16 01:35:57 +02:00
co-authored by Claude Opus 4.8
parent 6b9f910a14
commit b0751102d7
+9 -26
View File
@@ -38,6 +38,10 @@ logger = logging.getLogger(__name__)
_MAX_PURGE_DOC_TYPES = 64
def _bad_request(message: str) -> JSONResponse:
return JSONResponse({"error": "Bad request", "message": message}, status_code=400)
async def purge_doc_types_route(request: Request) -> JSONResponse:
"""POST /api/v1/vector-sync/purge — delete indexed vectors by doc type.
@@ -65,42 +69,21 @@ async def purge_doc_types_route(request: Request) -> JSONResponse:
body = await request.json()
except Exception as e:
logger.warning("Purge payload was not valid JSON: %s", e)
return JSONResponse(
{"error": "Bad request", "message": "invalid JSON"},
status_code=400,
)
return _bad_request("invalid JSON")
if not isinstance(body, dict):
return JSONResponse(
{"error": "Bad request", "message": "body must be a JSON object"},
status_code=400,
)
return _bad_request("body must be a JSON object")
raw = body.get("doc_types")
if raw is None:
return JSONResponse(
{"error": "Bad request", "message": "doc_types is required"},
status_code=400,
)
return _bad_request("doc_types is required")
if not isinstance(raw, list) or not all(isinstance(d, str) for d in raw):
return JSONResponse(
{
"error": "Bad request",
"message": "doc_types must be a list of strings",
},
status_code=400,
)
return _bad_request("doc_types must be a list of strings")
doc_types = [d for d in raw if d]
# Bound the batch: there are only a handful of real indexed types, so a huge
# list is abuse — cap it rather than fan out unbounded count+delete calls.
if len(doc_types) > _MAX_PURGE_DOC_TYPES:
return JSONResponse(
{
"error": "Bad request",
"message": f"doc_types exceeds the maximum of {_MAX_PURGE_DOC_TYPES}",
},
status_code=400,
)
return _bad_request(f"doc_types exceeds the maximum of {_MAX_PURGE_DOC_TYPES}")
try:
username, app_password = await get_basic_auth_for_user(user_id)