From 0895325b1c650ffdbc391ace285981191ba809f9 Mon Sep 17 00:00:00 2001 From: Aly Raffauf Date: Fri, 30 Jan 2026 20:15:12 -0500 Subject: [PATCH] server: add basic error handling for weather --- src/index.ts | 47 +++++++++++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 20 deletions(-) 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 }, + ); + } }, }, -- 2.51.2