Skip to content

Commit 114d6a3

Browse files
refactor: simplify API fetch error handling in aa-chains route
1 parent 0e26a88 commit 114d6a3

File tree

2 files changed

+31
-20
lines changed

2 files changed

+31
-20
lines changed

apps/portal/src/app/api/aa-chains/route.ts

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,38 @@ type ApiResponseType = {
99
};
1010

1111
export async function GET() {
12-
const [chainsWithServices, allChains] = await Promise.all([
13-
fetch("https://api.thirdweb.com/v1/chains/services", {
12+
const chainsWithServicesResponse = await fetch(
13+
"https://api.thirdweb.com/v1/chains/services",
14+
{
1415
headers: {
1516
"Content-Type": "application/json",
1617
},
17-
})
18-
.then((res) => res.json() as Promise<ApiResponseType>)
19-
.catch((error) => {
20-
console.error(error);
21-
return { data: {} as ApiResponseType["data"] };
22-
}),
23-
fetch("https://api.thirdweb.com/v1/chains", {
24-
headers: {
25-
"Content-Type": "application/json",
26-
},
27-
})
28-
.then((res) => res.json() as Promise<{ data: ChainMetadata[] }>)
29-
.catch((error) => {
30-
console.error(error);
31-
return { data: [] as ChainMetadata[] };
32-
}),
33-
]);
18+
},
19+
);
20+
21+
if (!chainsWithServicesResponse.ok) {
22+
throw new Error(
23+
`Failed to fetch chains with services ${chainsWithServicesResponse.status} - ${chainsWithServicesResponse.statusText}`,
24+
);
25+
}
26+
27+
const allChainsResponse = await fetch("https://api.thirdweb.com/v1/chains", {
28+
headers: {
29+
"Content-Type": "application/json",
30+
},
31+
});
32+
33+
if (!allChainsResponse.ok) {
34+
throw new Error(
35+
`Failed to fetch all chains ${allChainsResponse.status} - ${allChainsResponse.statusText}`,
36+
);
37+
}
38+
39+
const chainsWithServices =
40+
(await chainsWithServicesResponse.json()) as ApiResponseType;
41+
const allChains = (await allChainsResponse.json()) as {
42+
data: ChainMetadata[];
43+
};
3444

3545
const aaChains = Object.entries(chainsWithServices.data)
3646
.filter(([, services]) =>

apps/portal/src/components/Document/AAChainList.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ async function getChains(): Promise<ChainMetadata[]> {
77
try {
88
const chains = await fetch(`${getBaseUrl()}/api/aa-chains`);
99
if (!chains.ok) {
10+
console.error("Failed to fetch chains", chains.statusText);
1011
return [];
1112
}
1213
const result = (await chains.json()) as { data: ChainMetadata[] };
1314
return result.data;
1415
} catch (error) {
15-
console.error(error);
16+
console.error("Failed to fetch chains", error);
1617
return [];
1718
}
1819
}

0 commit comments

Comments
 (0)