fix: handle GitHub API error response in ContributorProfile#623
Conversation
✅ Deploy Preview for github-spy ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
Warning Review limit reached
More reviews will be available in 56 minutes and 49 seconds. Learn how PR review limits work. Your organization has run out of usage credits. Purchase more in the billing tab. ⌛ How to resolve this issue?After more reviews become available, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available. Please see our Fair Usage Limits Policy for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughContributorProfile's GitHub user data fetch now explicitly validates the response payload. When the GitHub API returns an error structure (indicated by the presence of ChangesGitHub API Error Handling
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes Possibly related issues
Possibly related PRs
Suggested labels
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds basic error handling to the GitHub user fetch in the ContributorProfile page so API error responses (e.g., rate limiting, user not found) are surfaced instead of being silently set as profile data.
Changes:
- Throws an error when the GitHub users API response contains a
messagefield.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
🎉 Thank you @Dhruvg0 for your contribution. Please make sure your PR follows https://github.com/GitMetricsLab/github_tracker/blob/main/CONTRIBUTING.md#-pull-request-guidelines
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/pages/ContributorProfile/ContributorProfile.tsx (1)
33-37:⚠️ Potential issue | 🔴 Critical | ⚡ Quick winCritical: Missing error handling for PR search will cause runtime crash.
The PR search endpoint can also return GitHub API error responses (e.g., rate limits, invalid queries), but unlike the user fetch, there's no error check here. If
prsData.itemsisundefined(because the response was an error object), line 79 will crash withTypeError: Cannot read property 'length' of undefined.🐛 Proposed fix
const prsRes = await fetch( `https://api.github.com/search/issues?q=author:${username}+type:pr` ); const prsData = await prsRes.json(); +if (prsData.message) throw new Error(prsData.message); setPRs(prsData.items);Or, applying the same HTTP status check pattern:
const prsRes = await fetch( `https://api.github.com/search/issues?q=author:${username}+type:pr` ); +if (!prsRes.ok) { + throw new Error(`GitHub API error: ${prsRes.status} ${prsRes.statusText}`); +} const prsData = await prsRes.json(); setPRs(prsData.items);🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/pages/ContributorProfile/ContributorProfile.tsx` around lines 33 - 37, The PR search fetch in ContributorProfile (variables prsRes, prsData, setPRs and the template using username) is missing HTTP/error handling; update the PR fetch to check prsRes.ok (or status) before reading prsData, and if the response is not ok or prsData.items is undefined, log or surface the error and call setPRs([]) (or a safe fallback) instead of using prsData.items directly so subsequent code doesn't crash when items is missing. Ensure you reuse the same error-check pattern used for the user fetch: await prsRes.json(), verify prsRes.ok, handle non-OK responses (including rate limit/error objects) and set a safe empty array via setPRs when necessary.
🧹 Nitpick comments (1)
src/pages/ContributorProfile/ContributorProfile.tsx (1)
28-30: ⚡ Quick winRecommend checking HTTP status code before parsing JSON.
While the
userData.messagecheck catches GitHub API error responses, it's more robust and idiomatic to verify the HTTP status code before parsing the response body. This approach also handles edge cases like network errors or malformed responses.🛡️ Proposed enhancement
const userRes = await fetch(`https://api.github.com/users/${username}`); +if (!userRes.ok) { + throw new Error(`GitHub API error: ${userRes.status} ${userRes.statusText}`); +} const userData = await userRes.json(); -if (userData.message) throw new Error(userData.message); setProfile(userData);🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/pages/ContributorProfile/ContributorProfile.tsx` around lines 28 - 30, Check the HTTP response status before parsing JSON in ContributorProfile.tsx: after awaiting fetch for `userRes` (the call using `username`), verify `userRes.ok` (or `userRes.status`) and throw a descriptive Error (include `userRes.status` and `userRes.statusText`) if it's not ok, then call `await userRes.json()` only for successful responses; update the error handling instead of relying on `userData.message`.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Outside diff comments:
In `@src/pages/ContributorProfile/ContributorProfile.tsx`:
- Around line 33-37: The PR search fetch in ContributorProfile (variables
prsRes, prsData, setPRs and the template using username) is missing HTTP/error
handling; update the PR fetch to check prsRes.ok (or status) before reading
prsData, and if the response is not ok or prsData.items is undefined, log or
surface the error and call setPRs([]) (or a safe fallback) instead of using
prsData.items directly so subsequent code doesn't crash when items is missing.
Ensure you reuse the same error-check pattern used for the user fetch: await
prsRes.json(), verify prsRes.ok, handle non-OK responses (including rate
limit/error objects) and set a safe empty array via setPRs when necessary.
---
Nitpick comments:
In `@src/pages/ContributorProfile/ContributorProfile.tsx`:
- Around line 28-30: Check the HTTP response status before parsing JSON in
ContributorProfile.tsx: after awaiting fetch for `userRes` (the call using
`username`), verify `userRes.ok` (or `userRes.status`) and throw a descriptive
Error (include `userRes.status` and `userRes.statusText`) if it's not ok, then
call `await userRes.json()` only for successful responses; update the error
handling instead of relying on `userData.message`.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 404e2760-9fd2-4b08-9e2d-78b2b8874be6
📒 Files selected for processing (1)
src/pages/ContributorProfile/ContributorProfile.tsx
|
🎉🎉 Thank you for your contribution! Your PR #623 has been merged! 🎉🎉 |
Related Issue
Description
When the GitHub API returns an error (e.g. user not found, rate limit exceeded),
the response is
{ message: 'Not Found' }instead of a valid user object.The component was accessing
profile.avatar_urlon this error object, causinga runtime crash. Added a guard to throw a proper error before any profile
properties are accessed.
How Has This Been Tested?
Manually tested by entering a non-existent GitHub username. Without the fix
the app crashes. With the fix an error is thrown and handled gracefully.
Screenshots (if applicable)
N/A
Type of Change
Summary by CodeRabbit