Skip to content

Commit b6e861e

Browse files
authored
Merge pull request #22 from ONS-Innovation/error-handling-improvements
Added Try catch blocks to API calls and removed nesting
2 parents debd1fa + 3ecd72f commit b6e861e

1 file changed

Lines changed: 25 additions & 15 deletions

File tree

github_api_toolkit/__init__.py

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,15 @@ def get_domain_email_by_user(self, username: str, org: str) -> list | tuple:
221221

222222
response = self.make_ql_request(self.query, self.params)
223223

224-
if response.status_code == 200:
225-
return response.json()["data"]["user"]["organizationVerifiedDomainEmails"]
226-
else:
224+
if response.status_code != 200:
227225
return self.get_error_message(response)
226+
227+
response_json = response.json()
228+
229+
if "errors" in response_json:
230+
return response_json["errors"][0].get("type", "No Error Type"), response_json["errors"][0].get("message", "No Error Message")
231+
232+
return response.json()["data"]["user"]["organizationVerifiedDomainEmails"]
228233

229234
def get_file_contents_from_repo(self, owner: str, repo: str, path: str, branch: str = "main") -> str:
230235
"""Gets the contents of a file from a GitHub Repository.
@@ -258,16 +263,15 @@ def get_file_contents_from_repo(self, owner: str, repo: str, path: str, branch:
258263

259264
response = self.make_ql_request(self.query, self.params)
260265

261-
if response.status_code == 200:
262-
try:
263-
contents = response.json()["data"]["repository"]["file"]["text"]
264-
return contents
265-
except TypeError:
266-
# If there is a type error, ["data"]["repository"]["file"] is None
267-
# Therefore, the file was not found
268-
return "File not found."
269-
else:
266+
if response.status_code != 200:
270267
return self.get_error_message(response)
268+
269+
try:
270+
return response.json()["data"]["repository"]["file"]["text"]
271+
except TypeError:
272+
# If there is a type error, ["data"]["repository"]["file"] is None
273+
# Therefore, the file was not found
274+
return "File not found."
271275

272276
def check_directory_for_file(self, owner: str, repo: str, path: str, branch: str) -> str | None:
273277
"""Checks if a file exists in a repository.
@@ -458,11 +462,17 @@ def get_team_maintainers(self, org: str, team_name: str) -> list | tuple:
458462

459463
response = self.make_ql_request(self.query, self.params)
460464

461-
if response.status_code == 200:
462-
return response.json()["data"]["organization"]["team"]["members"]["nodes"]
463-
else:
465+
if response.status_code != 200:
464466
return self.get_error_message(response)
465467

468+
try:
469+
return response.json()["data"]["organization"]["team"]["members"]["nodes"]
470+
except TypeError:
471+
# If there is a type error, ["data"]["organization"]["team"]["members"]["nodes"] is None
472+
# Therefore, the team was not found
473+
# Return an empty list
474+
return []
475+
466476
def get_codeowner_users(self, org: str, codeowners: list) -> list:
467477
"""Gets a list of users from a list of users and teams. Will get the maintainers of any teams and add them as a user.
468478

0 commit comments

Comments
 (0)