diff --git a/src/components/Weather.tsx b/src/components/Weather.tsx index cf94ffc..daa33a3 100644 --- a/src/components/Weather.tsx +++ b/src/components/Weather.tsx @@ -48,7 +48,14 @@ export function Weather() { const fetchWeather = (lat: number, lon: number) => { fetch(`/api/weather?lat=${lat}&lon=${lon}`) .then((response) => response.json()) - .then((data) => setWeather(data)); + .then((data) => { + if (data.error) { + setError(data.error); + } else { + setWeather(data); + } + }) + .catch(() => setError("Failed to load weather")); }; navigator.geolocation.getCurrentPosition( @@ -62,6 +69,14 @@ export function Weather() { ); }, []); + if (error) { + return ( +
+ {error} +
+ ); + } + if (!weather) { return
Loading weather...
; } diff --git a/src/index.ts b/src/index.ts index a7cd58b..cc94ee4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -103,11 +103,37 @@ const server = serve({ }, ); + if (!pointsResponse.ok) { + return Response.json( + { error: "Weather only available for US locations" }, + { status: 400 }, + ); + } + const pointsData = await pointsResponse.json(); - const forecastUrl = pointsData.properties.forecast; - const forecastResponse = await fetch(forecastUrl); - const forecastData = await forecastResponse.json(); + const forecastUrl = pointsData.properties?.forecast; + + if (!forecastUrl) { + return Response.json( + { error: "Weather only available for US locations" }, + { status: 400 }, + ); + } + + const forecastResponse = await fetch(forecastUrl, { + headers: { + "User-Agent": "(watsup, aly@aly.codes)", + }, + }); + + if (!forecastResponse.ok) { + return Response.json( + { error: "Weather forecast unavailable" }, + { status: 502 }, + ); + } + const forecastData = await forecastResponse.json(); const location = pointsData.properties.relativeLocation.properties; return Response.json({