Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise exceptions on graphql errors #2874

Merged
merged 2 commits into from
Jul 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions augur/tasks/github/util/github_graphql_data_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def __init__(self, response, message="Github Rate limit exceeded") -> None:

super().__init__(message)

class UrlNotFoundException(Exception):
class NotFoundException(Exception):
pass

class GithubGraphQlDataAccess:
Expand Down Expand Up @@ -72,14 +72,20 @@ def make_request(self, query, variables, timeout=40):

response = client.post(url=URL,auth=self.key_manager,json=json_dict, timeout=timeout)

if response.status_code in [403, 429]:
raise RatelimitException(response)

if response.status_code == 404:
raise UrlNotFoundException(f"404 for {URL} with query of {query}")

response.raise_for_status()

json_response = response.json()
if "errors" in json_response and len(json_response["errors"]) > 0:
errors = json_response["errors"]

not_found_error = self.__find_first_error_of_type(errors, "NOT_FOUND")

if not_found_error:
message = not_found_error.get("message", "Resource not found.")
raise NotFoundException(f"Could not find: {message}")

raise Exception(f"Github Graphql Data Access Errors: {errors}")

return response


Expand All @@ -93,7 +99,7 @@ def make_request_with_retries(self, query, variables, timeout=100):
except RetryError as e:
raise e.last_attempt.exception()

@retry(stop=stop_after_attempt(10), wait=wait_fixed(5), retry=retry_if_exception(lambda exc: not isinstance(exc, UrlNotFoundException)))
@retry(stop=stop_after_attempt(10), wait=wait_fixed(5), retry=retry_if_exception(lambda exc: not isinstance(exc, NotFoundException)))
def __make_request_with_retries(self, query, variables, timeout=40):
""" What method does?
1. Retires 10 times
Expand Down Expand Up @@ -203,3 +209,7 @@ def __get_total_count(self, data):
return int(data["totalCount"])
except ValueError as exc:
raise Exception(f"Error: totalCount is not an integer. Data: {data}") from exc

def __find_first_error_of_type(errors, type):

return next((error for error in errors if error.get("type").lower() == type.lower()), None)
Loading