Skip to content

Commit b318b2b

Browse files
committed
fix(github): add exception handling to get_user method
Wrap HTTP request and JSON parsing in try/except to handle network errors, timeouts, and missing keys gracefully. Returns None on failure instead of raising. Matches the pattern used in validate_token.
1 parent da59951 commit b318b2b

1 file changed

Lines changed: 20 additions & 15 deletions

File tree

backend/services/github.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import httpx
66
from typing import Optional
77
from dataclasses import dataclass
8+
from services.observability import logger
89

910
GITHUB_API_BASE = "https://api.github.com"
1011

@@ -62,21 +63,25 @@ async def validate_token(self) -> bool:
6263

6364
async def get_user(self) -> Optional[GitHubUser]:
6465
"""Get authenticated user info"""
65-
async with httpx.AsyncClient() as client:
66-
response = await client.get(
67-
f"{GITHUB_API_BASE}/user",
68-
headers=self.headers,
69-
timeout=10.0
70-
)
71-
if response.status_code != 200:
72-
return None
73-
data = response.json()
74-
return GitHubUser(
75-
login=data["login"],
76-
id=data["id"],
77-
avatar_url=data["avatar_url"],
78-
name=data.get("name")
79-
)
66+
try:
67+
async with httpx.AsyncClient() as client:
68+
response = await client.get(
69+
f"{GITHUB_API_BASE}/user",
70+
headers=self.headers,
71+
timeout=10.0
72+
)
73+
if response.status_code != 200:
74+
return None
75+
data = response.json()
76+
return GitHubUser(
77+
login=data.get("login", ""),
78+
id=data.get("id", 0),
79+
avatar_url=data.get("avatar_url"),
80+
name=data.get("name")
81+
)
82+
except (httpx.RequestError, httpx.TimeoutException, KeyError, ValueError) as e:
83+
logger.error("Failed to fetch GitHub user", error=str(e))
84+
return None
8085

8186
async def get_repos(
8287
self,

0 commit comments

Comments
 (0)