diff --git a/src/index.ts b/src/index.ts index a59c74e..a7cd58b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -93,27 +93,34 @@ const server = serve({ const lat = url.searchParams.get("lat"); const lon = url.searchParams.get("lon"); - const pointsResponse = await fetch( - `https://api.weather.gov/points/${lat},${lon}`, - { - headers: { - "User-Agent": "(watsup, aly@aly.codes)", + try { + const pointsResponse = await fetch( + `https://api.weather.gov/points/${lat},${lon}`, + { + headers: { + "User-Agent": "(watsup, aly@aly.codes)", + }, }, - }, - ); - - const pointsData = await pointsResponse.json(); - const forecastUrl = pointsData.properties.forecast; - const forecastResponse = await fetch(forecastUrl); - const forecastData = await forecastResponse.json(); - - const location = pointsData.properties.relativeLocation.properties; - - return Response.json({ - ...forecastData.properties.periods[0], - city: location.city, - state: location.state, - }); + ); + + const pointsData = await pointsResponse.json(); + const forecastUrl = pointsData.properties.forecast; + const forecastResponse = await fetch(forecastUrl); + const forecastData = await forecastResponse.json(); + + const location = pointsData.properties.relativeLocation.properties; + + return Response.json({ + ...forecastData.properties.periods[0], + city: location.city, + state: location.state, + }); + } catch (e) { + return Response.json( + { error: "Weather unavailable" }, + { status: 502 }, + ); + } }, },