Skip to content

Commit 255a320

Browse files
committed
added error handling to contributor load
1 parent 8c9712a commit 255a320

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

main.py

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,35 @@
66
def define_env(env):
77
@env.macro
88
def contributors(repo="mcloughlan/algo-notes"):
9-
USERS = requests.get(
10-
f"https://api.github.com/repos/{repo}/contributors").json()
11-
html = ""
12-
for u in USERS:
13-
html += f'<a href="https://github.com/{u["login"]}"><img src="https://github.com/{u["login"]}.png?size=50" alt="{u["login"]}" style="border-radius: 50%; margin: 5px;"></a>\n'
14-
return html
9+
try:
10+
url = f"https://api.github.com/repos/{repo}/contributors"
11+
resp = requests.get(
12+
url, headers={"Accept": "application/vnd.github+json"}, timeout=10)
13+
14+
if resp.status_code == 404:
15+
return f"<p style='color:red;'>Error: GitHub repo '{repo}' not found.</p>"
16+
17+
if resp.status_code == 403:
18+
return "<p style='color:red;'>Error: GitHub API rate limit exceeded. Try again later.</p>"
19+
20+
if not resp.ok:
21+
return f"<p style='color:red;'>Error: GitHub API returned status {resp.status_code}.</p>"
22+
23+
users = resp.json()
24+
if not isinstance(users, list):
25+
return "<p style='color:red;'>Error: Unexpected response format from GitHub API.</p>"
26+
27+
html = ""
28+
for u in users:
29+
if "login" not in u:
30+
continue # Skip invalid user object
31+
username = u["login"]
32+
html += (
33+
f'<a href="https://github.com/{username}" title="{username}">'
34+
f'<img src="https://github.com/{username}.png?size=50" alt="{username}" '
35+
f'style="border-radius: 50%; margin: 5px;"></a>\n'
36+
)
37+
return html or "<p>No contributors found.</p>"
38+
39+
except requests.exceptions.RequestException as e:
40+
return f"<p style='color:red;'>Error contacting GitHub: {e}</p>"

0 commit comments

Comments
 (0)