about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
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));