Skip to content

Commit

Permalink
fix: Handle partial failures when fetching raid ids
Browse files Browse the repository at this point in the history
  • Loading branch information
SvenKirschbaum committed Aug 16, 2024
1 parent 61d8c35 commit 4c74eb8
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions lambda/character-list/src/characters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ const lambdaHandler = async function (request: APIGatewayProxyEventV2 & SessionD
account.characters.filter((character: any) => character.level === MAX_LEVEL)
).flat();

let charactersRaidInfo;
let charactersRaidInfo = {};
try {
const characterRaidsResponses = await Promise.all(
const characterRaidsResponses = await Promise.allSettled(
maxLevelCharacters.map(async (character: any) => {
const raidResponse = await fetch(`https://${region}.api.blizzard.com/profile/wow/character/${character.realm.slug}/${character.name.toLowerCase()}/encounters/raids?namespace=profile-${region}&locale=en_US`, {
headers: {
Expand All @@ -63,11 +63,11 @@ const lambdaHandler = async function (request: APIGatewayProxyEventV2 & SessionD
});

if(!raidResponse.ok) {
logger.error(`Failed to fetch raid info for ${character.name}-${character.realm.slug}`, {
logger.error(`Unexpected response status when fetching raid info for ${character.name}-${character.realm.slug}`, {
status: raidResponse.status,
text: await raidResponse.text(),
});
throw new Error("Failed to fetch raid info");
throw new Error("Failed to fetch raid info for ${character.name}-${character.realm.slug}");
}

const raidResponseData = await raidResponse.json();
Expand All @@ -81,7 +81,11 @@ const lambdaHandler = async function (request: APIGatewayProxyEventV2 & SessionD
})
);

charactersRaidInfo = characterRaidsResponses.reduce((acc, character) => ({...acc, ...character}), {});
characterRaidsResponses.filter(res => res.status === "rejected").forEach((rejection) => {
logger.error("Partial failure when fetching raid info", rejection.reason as Error);
});

charactersRaidInfo = characterRaidsResponses.filter(res => res.status === "fulfilled").reduce((acc, character) => ({...acc, ...character}), {});
} catch (error) {
logger.error("Failed to fetch raid info", error as Error);
}
Expand Down

0 comments on commit 4c74eb8

Please sign in to comment.