Skip to content

Commit

Permalink
Fix #17: Improve error handling for README retrieval
Browse files Browse the repository at this point in the history
- Add repository existence check before fetching README
- Differentiate between missing repository and missing README errors
- Update error messages to be more descriptive
  • Loading branch information
jasminaaa20 committed Jan 16, 2025
1 parent b43f29a commit 57b5adb
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion backend/app/services/github_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ def _get_headers(self):
"X-GitHub-Api-Version": "2022-11-28"
}

def _check_repository_exists(self, username, repo):
"""
Check if the repository exists using the GitHub API.
"""
api_url = f"https://api.github.com/repos/{username}/{repo}"
response = requests.get(api_url, headers=self._get_headers())

if response.status_code == 404:
raise ValueError("Repository not found.")
elif response.status_code != 200:
raise Exception(f"Failed to check repository: {response.status_code}, {response.json()}")

def get_default_branch(self, username, repo):
"""Get the default branch of the repository."""
api_url = f"https://api.github.com/repos/{username}/{repo}"
Expand Down Expand Up @@ -159,12 +171,20 @@ def get_github_readme(self, username, repo):
Returns:
str: The contents of the README file.
Raises:
ValueError: If repository does not exist or has no README.
Exception: For other unexpected API errors.
"""
# First check if the repository exists
self._check_repository_exists(username, repo)

# Then attempt to fetch the README
api_url = f"https://api.github.com/repos/{username}/{repo}/readme"
response = requests.get(api_url, headers=self._get_headers())

if response.status_code == 404:
raise ValueError("Repository not found.")
raise ValueError("No README found for the specified repository.")
elif response.status_code != 200:
raise Exception(f"Failed to fetch README: {
response.status_code}, {response.json()}")
Expand Down

0 comments on commit 57b5adb

Please sign in to comment.