diff options
| author | Alexander_Kabui <alexanderkabua@gmail.com> | 2026-10-04 12:14:11 +0300 |
|---|---|---|
| committer | Alexander_Kabui <alexanderkabua@gmail.com> | 2026-10-05 21:53:57 +0300 |
| commit | 98b71eb8c080c21ca458961805019170c794fdc7 (patch) | |
| tree | c08c751223fb13f4721a5a95e161cefba5a8ee80 | |
| parent | 1b40aa1f557eadd8894996fff6081d2917e28f20 (diff) | |
| download | genenetwork4-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.
| -rw-r--r-- | src/routes/search.rs | 32 | ||||
| -rw-r--r-- | test/python/test_search.py | 8 | ||||
| -rw-r--r-- | tests/search.rs | 10 |
3 files changed, 25 insertions, 25 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)); diff --git a/test/python/test_search.py b/test/python/test_search.py index 1d2118e..d8c5cc5 100644 --- a/test/python/test_search.py +++ b/test/python/test_search.py @@ -24,10 +24,12 @@ class Gn3StubHandler(http.server.BaseHTTPRequestHandler): def do_GET(self): query = urllib.parse.parse_qs(urllib.parse.urlparse(self.path).query) if "&&" in query.get("query", [""])[0]: + # Mirrors production gn3's shape (live-checked Oct 4): + # diagnostic under "error", Python repr under "error_type". body = json.dumps( { - "error": "parsererror", - "error_message": "Syntax error in query: unexpected &&", + "error": "Syntax: <expression> AND <expression>", + "error_type": "b'QueryParserError'", } ).encode() self.send_response(http.client.BAD_REQUEST) @@ -91,7 +93,7 @@ class SearchTest(ServerTestCase): status, body = self.get("/search?q=a%26%26b") self.assertEqual(status, http.client.UNPROCESSABLE_ENTITY) self.assertEqual(body["code"], "invalid_query") - self.assertIn("Syntax error in query", body["detail"]) + self.assertIn("Syntax: <expression> AND <expression>", body["detail"]) def test_search_caps_limit(self): status, _ = self.get("/search?q=alcohol&limit=9999") diff --git a/tests/search.rs b/tests/search.rs index 7ba9791..5197ffd 100644 --- a/tests/search.rs +++ b/tests/search.rs @@ -25,12 +25,14 @@ const FIXTURE: &str = include_str!("../test/data/search_alcohol_phenotype.json") /// Stands in for gn3's /api/search during tests. async fn serve_gn3(Query(params): Query<HashMap<String, String>>) -> (StatusCode, String) { if params.get("query").is_some_and(|q| q.contains("&&")) { - // Mirrors gn3's shape for unparseable xapian queries. + // Mirrors production gn3's shape for rejected xapian queries + // (live-checked Oct 4): the diagnostic is under "error" and + // "error_type" leaks a Python repr. return ( StatusCode::BAD_REQUEST, json!({ - "error": "parsererror", - "error_message": "Syntax error in query: unexpected &&" + "error": "Syntax: <expression> AND <expression>", + "error_type": "b'QueryParserError'" }) .to_string(), ); @@ -99,7 +101,7 @@ async fn search_proxy_scenarios() { body["detail"] .as_str() .unwrap() - .contains("Syntax error in query") + .contains("Syntax: <expression> AND <expression>") ); // 4. limit is capped: ask for more than 100, the stub still sees a |
