From 98b71eb8c080c21ca458961805019170c794fdc7 Mon Sep 17 00:00:00 2001 From: Alexander_Kabui Date: Sun, 4 Oct 2026 12:14:11 +0300 Subject: * src/routes/search.rs (search): map any gn3 4xx with an error body to 422 Production gn3 puts the diagnostic under "error" and leaks Python reprs under "error_type", so the old "parsererror" match never fired and malformed queries fell to 502 with no actionable feedback. Match on "error" alone; gn3 5xx stays 502. * tests/search.rs test/python/test_search.py (search): stub replays production's error body shape. --- src/routes/search.rs | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/routes/search.rs b/src/routes/search.rs index 9ae568f..0b736b4 100644 --- a/src/routes/search.rs +++ b/src/routes/search.rs @@ -57,13 +57,10 @@ pub struct SearchHit { pub extra: std::collections::BTreeMap, } -/// gn3's error body for unparseable queries. -#[derive(Deserialize)] -struct Gn3ParseError { - error: String, - #[serde(rename = "error_message")] - error_message: String, -} +/// gn3's error body for rejected queries: {"error": "", +/// "error_type": "..."}. Live-checked against production (Oct 4): the +/// diagnostic lives under "error"; "error_type" leaks Python reprs +/// (e.g. "b'QueryParserError'"), so only "error" is load-bearing. #[utoipa::path(get, path = "/search", summary = "Search traits across GN datasets", @@ -149,21 +146,20 @@ pub async fn search( .await .map_err(|e| backend_error("backend_misbehaving", e.to_string()))?; - if status == reqwest::StatusCode::BAD_REQUEST { - // gn3 signals unparseable xapian queries as 400 + parsererror; - // forward its diagnostic so the caller can fix the query - // (assigned issue: report-invalid-search-queries-over-api). - if let Ok(Gn3ParseError { - error, - error_message, - }) = serde_json::from_str::(&body) - { - if error == "parsererror" { + if status.is_client_error() { + // gn3 signals rejected queries (xapian syntax errors, unknown + // type, ...) as 4xx with a JSON diagnostic; forward it as 422 + // invalid_query so the caller can fix the query (assigned issue: + // report-invalid-search-queries-over-api). Match on the presence + // of "error" alone -- production's "error_type" leaks Python + // reprs, so token-matching on it is fragile. + if let Ok(v) = serde_json::from_str::(&body) { + if let Some(diag) = v.get("error").and_then(|e| e.as_str()) { return Err(ApiError::new( StatusCode::UNPROCESSABLE_ENTITY, "invalid_query", "Query rejected by the search backend", - error_message, + diag.to_string(), "search", ) .with_request_id(rid)); -- cgit 1.4.1