Skip to content

Commit

Permalink
Merge pull request #2874 from chaoss/release-fixes
Browse files Browse the repository at this point in the history
Raise exceptions on graphql errors
  • Loading branch information
sgoggins authored Jul 23, 2024
2 parents 25cc00c + 1c8839f commit a951d13
Showing 1 changed file with 18 additions and 8 deletions.
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)

0 comments on commit a951d13

Please sign in to comment.