Skip to content

Commit

Permalink
Improve exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
muellerberndt committed Jan 13, 2025
1 parent 6ba1504 commit 6496236
Showing 1 changed file with 24 additions and 17 deletions.
41 changes: 24 additions & 17 deletions src/util/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,30 @@ async def fetch_github_file(url: str, target_path: str) -> None:
- https://github.com/owner/repo/tree/branch/path/to/file
target_path: Path where to store the file
"""
# Convert web URL to raw content URL
raw_url = url.replace("github.com", "raw.githubusercontent.com").replace("/blob/", "/").replace("/tree/", "/")

# Fetch file content
headers = await get_headers()
async with aiohttp.ClientSession() as session:
async with session.get(raw_url, headers=headers) as response:
if response.status != 200:
raise Exception(f"GitHub fetch error: Status code {response.status} for URL {raw_url}")
content = await response.text()

# Create target directory if it doesn't exist
os.makedirs(os.path.dirname(target_path), exist_ok=True)

# Write the content to the file
async with aiofiles.open(target_path, "w") as f:
await f.write(content)
try:
# Convert web URL to raw content URL
raw_url = url.replace("github.com", "raw.githubusercontent.com").replace("/blob/", "/").replace("/tree/", "/")

# Fetch file content
headers = await get_headers()
async with aiohttp.ClientSession() as session:
async with session.get(raw_url, headers=headers) as response:
if response.status != 200:
raise Exception(f"GitHub fetch error: Status code {response.status} for URL {raw_url}")
content = await response.text()

# Create target directory if it doesn't exist
os.makedirs(os.path.dirname(target_path), exist_ok=True)

# Write the content to the file
async with aiofiles.open(target_path, "w") as f:
await f.write(content)

return None # Explicit return to ensure coroutine

except Exception as e:
logger.error(f"Error fetching GitHub file {url}: {str(e)}")
raise # Re-raise to handle in caller


async def fetch_github_repo(url: str, target_path: str) -> None:
Expand Down

0 comments on commit 6496236

Please sign in to comment.