Skip to content

Commit 57b5adb

Browse files
committed
Fix #17: Improve error handling for README retrieval
- Add repository existence check before fetching README - Differentiate between missing repository and missing README errors - Update error messages to be more descriptive
1 parent b43f29a commit 57b5adb

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

backend/app/services/github_service.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,18 @@ def _get_headers(self):
7777
"X-GitHub-Api-Version": "2022-11-28"
7878
}
7979

80+
def _check_repository_exists(self, username, repo):
81+
"""
82+
Check if the repository exists using the GitHub API.
83+
"""
84+
api_url = f"https://api.github.com/repos/{username}/{repo}"
85+
response = requests.get(api_url, headers=self._get_headers())
86+
87+
if response.status_code == 404:
88+
raise ValueError("Repository not found.")
89+
elif response.status_code != 200:
90+
raise Exception(f"Failed to check repository: {response.status_code}, {response.json()}")
91+
8092
def get_default_branch(self, username, repo):
8193
"""Get the default branch of the repository."""
8294
api_url = f"https://api.github.com/repos/{username}/{repo}"
@@ -159,12 +171,20 @@ def get_github_readme(self, username, repo):
159171
160172
Returns:
161173
str: The contents of the README file.
174+
175+
Raises:
176+
ValueError: If repository does not exist or has no README.
177+
Exception: For other unexpected API errors.
162178
"""
179+
# First check if the repository exists
180+
self._check_repository_exists(username, repo)
181+
182+
# Then attempt to fetch the README
163183
api_url = f"https://api.github.com/repos/{username}/{repo}/readme"
164184
response = requests.get(api_url, headers=self._get_headers())
165185

166186
if response.status_code == 404:
167-
raise ValueError("Repository not found.")
187+
raise ValueError("No README found for the specified repository.")
168188
elif response.status_code != 200:
169189
raise Exception(f"Failed to fetch README: {
170190
response.status_code}, {response.json()}")

0 commit comments

Comments
 (0)