Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,18 +199,22 @@ async function fetchWeatherData(city) {
hideWeather();

try {
const [currentResponse, forecastResponse] = await Promise.all([
fetch(`${API_BASE}/weather?q=${encodeURIComponent(city)}&units=metric`),
fetch(`${API_BASE}/forecast?q=${encodeURIComponent(city)}&units=metric`)
]);
let currentResponse, forecastResponse;
try {
[currentResponse, forecastResponse] = await Promise.all([
fetch(`${API_BASE}/weather?q=${encodeURIComponent(city)}&units=metric`),
fetch(`${API_BASE}/forecast?q=${encodeURIComponent(city)}&units=metric`)
]);
} catch (networkError) {
throw new Error('Network error, please try again');
}

if (!currentResponse.ok) {
const errorData = await parseJsonSafe(currentResponse);
throw new Error(errorData?.message || 'City not found');
if (currentResponse.status === 404) {
throw new Error('City not found');
}
if (!forecastResponse.ok) {
const errorData = await parseJsonSafe(forecastResponse);
throw new Error(errorData?.message || 'Forecast unavailable');

if (!currentResponse.ok || !forecastResponse.ok) {
throw new Error('Something went wrong');
}

const currentData = await currentResponse.json();
Expand All @@ -221,7 +225,7 @@ async function fetchWeatherData(city) {
showWeather();
} catch (error) {
console.error('Fetch error:', error);
showError(error.message || 'Unable to fetch weather data. Please try again.');
showError(error.message);
} finally {
hideLoading();
}
Expand Down