Skip to content

Commit

Permalink
chore: improve logs in dataApi catch
Browse files Browse the repository at this point in the history
  • Loading branch information
dvaJi committed Oct 6, 2024
1 parent 1f8ccdf commit 22d7626
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 52 deletions.
95 changes: 46 additions & 49 deletions app/[lang]/(genshin)/character/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,62 +129,17 @@ export default async function GenshinCharacterPage({ params }: Props) {
"genshin",
"character"
);
const [
beta,
_buildsOld,
mubuild,
teams,
_characters,
weaponsList,
artifactsList,
] = await Promise.all([

const [beta, _character] = await Promise.all([
getData<Beta>("genshin", "beta"),
getGenshinData<Build>({
resource: "builds",
language: langData as any,
filter: {
id: params.id,
},
}),
getGenshinData<MostUsedBuild>({
resource: "mostUsedBuilds",
getGenshinData<Character>({
resource: "characters",
language: langData as any,
filter: {
id: params.id,
},
}),
getGenshinData<Teams>({
resource: "teams",
language: langData,
filter: {
id: params.id,
},
}),
getGenshinData<Character[]>({
resource: "characters",
language: langData as any,
select: ["id", "name", "rarity", "element"],
}),
getGenshinData<Record<string, Weapon>>({
resource: "weapons",
language: langData as any,
select: ["id", "name", "rarity", "stats"],
asMap: true,
}),
getGenshinData<Record<string, Artifact>>({
resource: "artifacts",
language: langData as any,
select: ["id", "name", "max_rarity", "two_pc", "four_pc"],
asMap: true,
}),
]);
const _character = await getGenshinData<Character>({
resource: "characters",
language: langData as any,
filter: {
id: params.id,
},
});
const _betaCharacter = beta[locale]?.characters?.find(
(c: any) => c.id === params.id
);
Expand All @@ -199,6 +154,48 @@ export default async function GenshinCharacterPage({ params }: Props) {
return notFound();
}

const [_buildsOld, mubuild, teams, _characters, weaponsList, artifactsList] =
await Promise.all([
getGenshinData<Build>({
resource: "builds",
language: langData as any,
filter: {
id: params.id,
},
}),
getGenshinData<MostUsedBuild>({
resource: "mostUsedBuilds",
language: langData as any,
filter: {
id: params.id,
},
}),
getGenshinData<Teams>({
resource: "teams",
language: langData,
filter: {
id: params.id,
},
}),
getGenshinData<Character[]>({
resource: "characters",
language: langData as any,
select: ["id", "name", "rarity", "element"],
}),
getGenshinData<Record<string, Weapon>>({
resource: "weapons",
language: langData as any,
select: ["id", "name", "rarity", "stats"],
asMap: true,
}),
getGenshinData<Record<string, Artifact>>({
resource: "artifacts",
language: langData as any,
select: ["id", "name", "max_rarity", "two_pc", "four_pc"],
asMap: true,
}),
]);

const buildsOld = _buildsOld || { builds: [] };

let weapons: Record<string, Weapon> = {};
Expand Down
6 changes: 4 additions & 2 deletions lib/dataApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ async function getData<T>(
if (options.filter && res.status === 404) return null as T;

if (!res.ok) {
console.error("Error fetching data", res.statusText, res.text());
const text = await res.text();
console.error("Error fetching data", res.statusText, text);
throw new Error(res.statusText);
}

Expand All @@ -78,14 +79,15 @@ async function getData<T>(

return res.json() as Promise<T>;
} catch (error) {
console.log(error);
console.log(i, error);
if (
error instanceof SyntaxError &&
error.message.includes("Bad Gateway") &&
i < MAX_RETRIES - 1
) {
continue; // if it's a "Bad Gateway" error and we haven't reached the max retries, retry
} else {
console.error({ options });
console.error("Error fetching data", error);
throw error; // if it's a different error or we've reached the max retries, throw the error
}
Expand Down
3 changes: 2 additions & 1 deletion lib/news.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ export async function getNews(game: string) {
});

if (!res.ok) {
console.error("Error fetching news:", res.statusText);
const text = await res.text();
console.error("Error fetching news:", res.statusText, text);
return [];
}

Expand Down

0 comments on commit 22d7626

Please sign in to comment.