about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorAlexander_Kabui <alexanderkabua@gmail.com>2026-10-04 12:14:11 +0300
committerAlexander_Kabui <alexanderkabua@gmail.com>2026-10-05 21:53:57 +0300
commit98b71eb8c080c21ca458961805019170c794fdc7 (patch)
treec08c751223fb13f4721a5a95e161cefba5a8ee80 /src
parent1b40aa1f557eadd8894996fff6081d2917e28f20 (diff)
downloadgenenetwork4-98b71eb8c080c21ca458961805019170c794fdc7.tar.gz
* 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.
Diffstat (limited to 'src')
-rw-r--r--src/routes/search.rs32
1 files changed, 14 insertions, 18 deletions
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<String, serde_json::Value>,
 }
 
-/// 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": "<diagnostic>",
+/// "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::<Gn3ParseError>(&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::<serde_json::Value>(&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));