Skip to content

Commit

Permalink
Add error handling for invalid queries (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
J535D165 authored Dec 11, 2022
1 parent 8015738 commit b6a741f
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion pyalex/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ def invert_abstract(inv_index):
return " ".join(map(lambda x: x[0], sorted(l, key=lambda x: x[1])))


class QueryError(ValueError):
pass


class OpenAlexEntity(dict):

pass
Expand Down Expand Up @@ -139,6 +143,20 @@ def _get_multi_items(self, record_list):

return self.filter(openalex_id="|".join(record_list)).get()

def __getattr__(self, key):

if key == "groupby":
raise AttributeError(
"Object has no attribute 'groupby'. "
"Did you mean 'group_by'?")

if key == "filter_search":
raise AttributeError(
"Object has no attribute 'filter_search'. "
"Did you mean 'search_filter'?")

return getattr(self, key)

def __getitem__(self, record_id):

if isinstance(record_id, list):
Expand Down Expand Up @@ -177,9 +195,13 @@ def get(self, return_meta=False, page=None, per_page=None, cursor=None):
res = requests.get(
url, headers={"User-Agent": "pyalex/" + __version__, "email": config.email}
)
res.raise_for_status()
res_json = res.json()

if res.status_code == 403 and "query parameters" in res_json["error"]:
raise QueryError(res_json["message"])

res.raise_for_status()

# group-by or results page
if "group-by" in self.params:
results = res_json["group_by"]
Expand Down

0 comments on commit b6a741f

Please sign in to comment.