Skip to content
This repository has been archived by the owner on Nov 22, 2023. It is now read-only.

Commit

Permalink
search: Fix App Search error handling in benchmark script
Browse files Browse the repository at this point in the history
  • Loading branch information
dbaty committed Sep 10, 2021
1 parent 48ad782 commit 9c1ce86
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/pcapi/scripts/benchmark_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import jinja2
import pytz
import requests
import requests.exceptions
import simplejson.errors
import yaml

from pcapi.utils import human_ids
Expand Down Expand Up @@ -238,23 +240,30 @@ def search(self, description, criteria):
"group": {"field": "group"},
"sort": self.sort,
}
response = requests.get(self.url, headers=self.headers, json=query)
out = response.json()
if "errors" in out:
try:
response = requests.get(self.url, headers=self.headers, json=query, timeout=30)
out = response.json()
error_details = out.get("errors")
except (requests.exceptions.RequestException, simplejson.errors.JSONDecodeError) as exc:
response = None
error_details = str(exc)
if not response or not response.ok:
print(f'[App Search] Error for "{description}" with the following query: ')
pprint.pprint(query)
print(out["errors"])
return []
print(error_details)
results = []
else:
results = out["results"]
return ResultSet(
elapsed=response.elapsed.total_seconds(),
elapsed=response.elapsed.total_seconds() if response else 0,
results=[
Result(
id=int(result["id"]["raw"]),
score=result["_meta"]["score"],
name=result["name"]["raw"],
full={key: value["raw"] for key, value in result.items() if not key.startswith("_")},
)
for result in out["results"]
for result in results
],
)

Expand Down

0 comments on commit 9c1ce86

Please sign in to comment.