From 7f707aaca67e21b43d4c9f29a460effb5129c9a4 Mon Sep 17 00:00:00 2001 From: John Nduli Date: Thu, 4 Jul 2024 19:14:39 +0300 Subject: fix: cleaner handling for gsearch errors --- gn2/wqflask/gsearch.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'gn2') diff --git a/gn2/wqflask/gsearch.py b/gn2/wqflask/gsearch.py index cad6db94..2a214cf8 100644 --- a/gn2/wqflask/gsearch.py +++ b/gn2/wqflask/gsearch.py @@ -28,10 +28,16 @@ class GSearch: hmac = curry(3, lambda trait_name, dataset, data_hmac: f"{trait_name}:{dataset}:{data_hmac}") convert_lod = lambda x: x / 4.61 self.trait_list = [] - for i, trait in enumerate(requests.get( + response = requests.get( urljoin(GN3_LOCAL_URL, "/api/search?" + urlencode({"query": self.terms, "type": self.type, - "per_page": MAX_SEARCH_RESULTS}))).json()): + "per_page": MAX_SEARCH_RESULTS}))) + if response.status_code == 400 and "parsererror" in response.text.lower(): + raise ValueError(f"Query `{self.terms}` has a problem: {response.json()}") + response.raise_for_status() + response_json = response.json() + + for i, trait in enumerate(response_json): trait = MonadicDict(trait) trait["index"] = Just(i) trait["location_repr"] = (Maybe.apply(chr_mb) -- cgit v1.2.3