Skip to content

Commit 65ccb12

Browse files
tlongwell-blocknpub1t2tgm7d8f995uqvmnm8h88sg3wnpp9a5xysjf6dg3tjmgt3ltulqdp8ehr
andauthored
Parse Typesense multi_search errors (#1208)
Signed-off-by: npub1t2tgm7d8f995uqvmnm8h88sg3wnpp9a5xysjf6dg3tjmgt3ltulqdp8ehr <5a968df9a7494b4e019b9ecf739e088ba61097b4312124e9a88ae5b42e3f5f3e@sprout-oss.stage.blox.sqprod.co> Co-authored-by: npub1t2tgm7d8f995uqvmnm8h88sg3wnpp9a5xysjf6dg3tjmgt3ltulqdp8ehr <5a968df9a7494b4e019b9ecf739e088ba61097b4312124e9a88ae5b42e3f5f3e@sprout-oss.stage.blox.sqprod.co>
1 parent 89ff950 commit 65ccb12

1 file changed

Lines changed: 79 additions & 3 deletions

File tree

‎crates/buzz-search/src/query.rs‎

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,20 @@ pub struct SearchResult {
100100

101101
#[derive(Debug, Deserialize)]
102102
struct TypesenseMultiSearchResponse {
103-
results: Vec<TypesenseSearchResponse>,
103+
results: Vec<TypesenseSearchResult>,
104+
}
105+
106+
#[derive(Debug, Deserialize)]
107+
#[serde(untagged)]
108+
enum TypesenseSearchResult {
109+
Ok(TypesenseSearchResponse),
110+
Error(TypesenseSearchError),
111+
}
112+
113+
#[derive(Debug, Deserialize)]
114+
struct TypesenseSearchError {
115+
code: u16,
116+
error: String,
104117
}
105118

106119
#[derive(Debug, Deserialize)]
@@ -175,13 +188,22 @@ pub async fn search(
175188
return Err(SearchError::Api { status, body });
176189
}
177190

178-
// multi_search wraps results: {"results": [<search_response>]}
191+
// multi_search wraps results: {"results": [<search_response>]}. Individual
192+
// searches can fail inside an HTTP 200 response as `{code, error}`; surface
193+
// those as API errors instead of deserializing them as JSON errors so callers
194+
// and logs show the actual Typesense failure.
179195
let wrapper: TypesenseMultiSearchResponse = resp.json().await?;
180196
let ts_resp = wrapper.results.into_iter().next().ok_or(SearchError::Api {
181197
status: 200,
182198
body: "empty multi_search results".into(),
183199
})?;
184-
parse_response(ts_resp)
200+
match ts_resp {
201+
TypesenseSearchResult::Ok(response) => parse_response(response),
202+
TypesenseSearchResult::Error(error) => Err(SearchError::Api {
203+
status: error.code,
204+
body: error.error,
205+
}),
206+
}
185207
}
186208

187209
fn parse_response(ts_resp: TypesenseSearchResponse) -> Result<SearchResult, SearchError> {
@@ -332,4 +354,58 @@ mod tests {
332354
assert_eq!(result.found, 0);
333355
assert!(result.hits.is_empty());
334356
}
357+
358+
#[test]
359+
fn test_multi_search_result_success_parses() {
360+
let raw = json!({
361+
"results": [{
362+
"found": 1,
363+
"page": 1,
364+
"hits": [{
365+
"document": {
366+
"id": "abc123",
367+
"content": "hello buzz",
368+
"kind": 1,
369+
"pubkey": "deadbeef",
370+
"channel_id": "chan-uuid",
371+
"created_at": 1700000000i64,
372+
"tags_flat": []
373+
},
374+
"text_match": 578730123i64
375+
}]
376+
}]
377+
});
378+
379+
let wrapper: TypesenseMultiSearchResponse =
380+
serde_json::from_value(raw).expect("should parse multi_search success result");
381+
let response = match wrapper.results.into_iter().next().expect("one result") {
382+
TypesenseSearchResult::Ok(response) => response,
383+
TypesenseSearchResult::Error(err) => panic!("expected success result, got {err:?}"),
384+
};
385+
386+
let result = parse_response(response).expect("should parse response");
387+
assert_eq!(result.found, 1);
388+
assert_eq!(result.hits.len(), 1);
389+
assert_eq!(result.hits[0].event_id, "abc123");
390+
}
391+
392+
#[test]
393+
fn test_multi_search_result_error_parses() {
394+
let raw = json!({
395+
"results": [{
396+
"code": 400,
397+
"error": "Could not find a filter field named `channel_id` in the schema."
398+
}]
399+
});
400+
401+
let wrapper: TypesenseMultiSearchResponse =
402+
serde_json::from_value(raw).expect("should parse multi_search error result");
403+
let err = match wrapper.results.into_iter().next().expect("one result") {
404+
TypesenseSearchResult::Ok(_) => panic!("expected error result"),
405+
TypesenseSearchResult::Error(err) => err,
406+
};
407+
408+
assert_eq!(err.code, 400);
409+
assert!(err.error.contains("channel_id"));
410+
}
335411
}

0 commit comments

Comments
 (0)