From 76b91cfb7cfe529d53729894ee0e8c403b614631 Mon Sep 17 00:00:00 2001 From: "Tomi P. Hakala" Date: Sun, 5 Jan 2025 17:37:09 +0200 Subject: [PATCH] fix: add safety check for empty weather data response in FetchWeather method - Implemented a safety check to handle cases where no weather conditions are returned from the OpenWeather API, preventing potential nil pointer dereferences and improving error handling. --- internal/weather/provider_openweather.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/internal/weather/provider_openweather.go b/internal/weather/provider_openweather.go index 460e409..63b07f3 100644 --- a/internal/weather/provider_openweather.go +++ b/internal/weather/provider_openweather.go @@ -105,6 +105,11 @@ func (p *OpenWeatherProvider) FetchWeather(settings *conf.Settings) (*WeatherDat break } + // Safety check for weather data + if len(weatherData.Weather) == 0 { + return nil, fmt.Errorf("no weather conditions returned from API") + } + return &WeatherData{ Time: time.Unix(weatherData.Dt, 0), Location: Location{