From a3e139b67ddb9ab3d1078ad1b5b33d8a3cfe987f Mon Sep 17 00:00:00 2001 From: Mayuri Gade Date: Mon, 1 Jun 2026 11:27:58 +0530 Subject: [PATCH] fix(contributor): distinguish API errors from user not found --- .../ContributorProfile/ContributorProfile.tsx | 47 +++++++++++++++---- 1 file changed, 39 insertions(+), 8 deletions(-) diff --git a/src/pages/ContributorProfile/ContributorProfile.tsx b/src/pages/ContributorProfile/ContributorProfile.tsx index cab7fe03..fc39951c 100644 --- a/src/pages/ContributorProfile/ContributorProfile.tsx +++ b/src/pages/ContributorProfile/ContributorProfile.tsx @@ -14,26 +14,40 @@ type Profile = { bio: string; }; +// --- FIX: Added separate error type to distinguish 404 vs other failures --- +type ErrorType = "not_found" | "api_error" | null; + export default function ContributorProfile() { const { username } = useParams(); const [profile, setProfile] = useState(null); const [prs, setPRs] = useState([]); const [loading, setLoading] = useState(true); + // --- FIX: New error state --- + const [error, setError] = useState(null); useEffect(() => { async function fetchData() { if (!username) return; + // --- FIX: Reset error on each fetch --- + setError(null); + try { const userRes = await fetch(`https://api.github.com/users/${username}`); + if (!userRes.ok) { if (userRes.status === 404) { - setProfile(null); - setPRs([]); - return; + // --- FIX: 404 → "not_found", not a generic error --- + setError("not_found"); + } else { + // --- FIX: Rate limit, server error, etc. → "api_error" --- + setError("api_error"); } - throw new Error(`Failed to load user (${userRes.status})`); + setProfile(null); + setPRs([]); + return; } + const userData = await userRes.json(); setProfile(userData as Profile); @@ -41,7 +55,6 @@ export default function ContributorProfile() { `https://api.github.com/search/issues?q=author:${username}+type:pr` ); if (!prsRes.ok) { - // avoid crashing — show empty PR list and notify user setPRs([]); toast.error("Failed to load pull requests."); } else { @@ -49,6 +62,10 @@ export default function ContributorProfile() { setPRs(prsData.items ?? []); } } catch { + // --- FIX: Network/connection failures → "api_error" --- + setError("api_error"); + setProfile(null); + setPRs([]); toast.error("Failed to fetch user data."); } finally { setLoading(false); @@ -65,10 +82,24 @@ export default function ContributorProfile() { if (loading) return
Loading...
; - if (!profile) + // --- FIX: Show correct message based on error type --- + if (error === "not_found") { return ( -
User not found.
+
+ User not found. +
); + } + + if (error === "api_error") { + return ( +
+ Unable to load contributor profile. Please try again later. +
+ ); + } + + if (!profile) return null; return (
@@ -112,4 +143,4 @@ export default function ContributorProfile() { )}
); -} +} \ No newline at end of file