Skip to content

Commit

Permalink
Added error handling to countryData fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
Ales Zima committed Jun 12, 2024
1 parent f79674d commit 3738240
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@ import Homepage from '../components/homepage/Homepage';
import type { Country } from '../types/country';
async function fetchCountryData() {
async function fetchCountryData(): Promise<Country[]> {
const url = new URL(
'https://esosek.github.io/rest-countries/api/country.json'
);
const response = await fetch(url);
if (!response.ok) {
throw new Error('Failed to fetch country data');
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error('Failed to fetch country data');
}
const responseData = await response.json();
return responseData.body;
} catch (error) {
console.log(error);
return [];
}
const responseData = await response.json();
return responseData.body as Country[];
}
const countryData = await fetchCountryData();
Expand Down

0 comments on commit 3738240

Please sign in to comment.