From 7f102c9ebc94b6a4887846b6d716e1c07432d0d5 Mon Sep 17 00:00:00 2001 From: juls0730 <62722391+juls0730@users.noreply.github.com> Date: Tue, 19 Nov 2024 01:55:24 -0600 Subject: [PATCH] more error checking --- main.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index ba22fd7..ada085d 100644 --- a/main.go +++ b/main.go @@ -159,7 +159,9 @@ type OpenWeatherResponse struct { } `json:"weather"` Main struct { Temp float64 `json:"temp"` - } + } `json:"main"` + Code int `json:"cod"` + Message string `json:"message"` } type WeatherData struct { @@ -181,6 +183,11 @@ type WeatherCache struct { } func NewWeatherCache() *WeatherCache { + if os.Getenv("OPENWEATHER_API_KEY") == "" || os.Getenv("OPENWEATHER_LAT") == "" || os.Getenv("OPENWEATHER_LON") == "" { + log.Fatalln("OpenWeather API Key, and your latitude and longitude are required!") + return nil + } + updateInterval, err := strconv.Atoi(os.Getenv("OPENWEATHER_UPDATE_INTERVAL")) if err != nil || updateInterval < 1 { updateInterval = 15 @@ -251,6 +258,16 @@ func (c *WeatherCache) updateWeather() { return } + // if the request failed + if weatherResp.Code != 200 { + // if there is no pre-existing data in the cache + if c.data.WeatherText == "" { + log.Fatalf("Fetching the weather data failed!\n%s\n", weatherResp.Message) + } else { + return + } + } + c.mutex.Lock() c.data.Temperature = weatherResp.Main.Temp c.data.WeatherText = weatherResp.Weather[0].Name