Skip to content

Commit

Permalink
Improve error handling and add safeguards for match data in league co…
Browse files Browse the repository at this point in the history
…mponents
  • Loading branch information
danblock97 committed Feb 17, 2025
1 parent 9b26df7 commit 5613bed
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 14 deletions.
10 changes: 7 additions & 3 deletions src/app/league/profile/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,17 @@ const ProfilePageContent = () => {
},
body: JSON.stringify({ gameName, tagLine, region }),
});
const data = await response.json();
if (!response.ok) {
throw new Error("Failed to trigger update");
console.error("API update error:", data);
throw new Error(
data.error || `Failed to trigger update: ${response.status}`
);
}
await response.json();
// Removed the extra await response.json() call.
fetchProfileData();
} catch (error) {
console.error("Error triggering update:", error);
console.error("Error triggering update:", error.message);
dispatch({ type: "FETCH_FAILURE", payload: error.message });
} finally {
dispatch({ type: "UPDATE_END" });
Expand Down
17 changes: 13 additions & 4 deletions src/components/league/Last20GamesPerformance.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ const Last20GamesPerformance = ({
// Filter to get the last 20 matches of the selected player
const last20Matches = useMemo(() => {
return matchDetails
.filter((match) =>
match.info.participants.some(
(participant) => participant.puuid === selectedSummonerPUUID
)
.filter(
(match) =>
match &&
match.info &&
match.info.participants &&
match.info.participants.some(
(participant) => participant.puuid === selectedSummonerPUUID
)
)
.slice(0, 20);
}, [matchDetails, selectedSummonerPUUID]);
Expand All @@ -28,10 +32,15 @@ const Last20GamesPerformance = ({
const championPerformance = {};

last20Matches.forEach((match) => {
// Safe guard in case match.info.participants is missing
if (!match || !match.info || !match.info.participants) return;

const currentPlayer = match.info.participants.find(
(participant) => participant.puuid === selectedSummonerPUUID
);

if (!currentPlayer) return;

// Win/Loss
if (currentPlayer.win) totalWins++;

Expand Down
5 changes: 3 additions & 2 deletions src/components/league/MatchHistory.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,9 @@ const MatchHistory = ({

// Update filteredMatches to include champion filter
const filteredMatches = matchDetails.filter((match) => {
const participants = match.info && match.info.participants;
if (!participants) return false;
if (!match || !match.info || !match.info.participants) return false;

const participants = match.info.participants;
const currentPlayer = participants.find(
(participant) => participant.puuid === selectedSummonerPUUID
);
Expand All @@ -354,6 +354,7 @@ const MatchHistory = ({
if (selectedQueue && match.info.queueId !== selectedQueue) {
return false;
}

return true;
});

Expand Down
3 changes: 3 additions & 0 deletions src/components/league/RecentlyPlayedWith.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ const RecentlyPlayedWith = ({
const teammateStats = {};

matchDetails.forEach((match) => {
// Guard: ensure match, match.info, and match.info.participants exist
if (!match || !match.info || !match.info.participants) return;

const currentPlayer = match.info.participants.find(
(participant) => participant.puuid === selectedSummonerPUUID
);
Expand Down
13 changes: 8 additions & 5 deletions src/lib/league/leagueApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ export const fetchSummonerData = async (encryptedPUUID, region) => {
* Fetch champion mastery data.
*/
export const fetchChampionMasteryData = async (encryptedPUUID, region) => {
const championMasteryResponse = await fetch(
`https://${region}.api.riotgames.com/lol/champion-mastery/v4/champion-masteries/by-puuid/${encryptedPUUID}`,
{ headers: { "X-Riot-Token": RIOT_API_KEY } }
);
const url = `https://${region}.api.riotgames.com/lol/champion-mastery/v4/champion-masteries/by-puuid/${encryptedPUUID}`;
const championMasteryResponse = await fetch(url, {
headers: { "X-Riot-Token": RIOT_API_KEY },
});
if (!championMasteryResponse.ok) {
throw new Error("Failed to fetch champion mastery data");
const errorText = await championMasteryResponse.text();
throw new Error(
`Failed to fetch champion mastery data. Status: ${championMasteryResponse.status} - ${errorText}`
);
}
const masteryData = await championMasteryResponse.json();
return masteryData.slice(0, 6);
Expand Down

0 comments on commit 5613bed

Please sign in to comment.