1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

Gracefully handle weather data outages

This commit is contained in:
Chris Cummer
2018-04-08 07:35:44 -07:00
committed by Chris Cummer
parent 01aab4cc53
commit 204e3b4032
3 changed files with 19 additions and 9 deletions

View File

@@ -11,18 +11,20 @@ import (
func Fetch(cityID int) *owm.CurrentWeatherData {
apiKey := os.Getenv("WTF_OWM_API_KEY")
return currentWeather(apiKey, cityID)
data, _ := currentWeather(apiKey, cityID)
return data
}
/* -------------------- Unexported Functions -------------------- */
func currentWeather(apiKey string, cityCode int) *owm.CurrentWeatherData {
func currentWeather(apiKey string, cityCode int) (*owm.CurrentWeatherData, error) {
weather, err := owm.NewCurrent(Config.UString("wtf.weather.tempUnit", "C"), Config.UString("wtf.weather.language", "EN"), apiKey)
if err != nil {
panic(err)
return nil, err
}
weather.CurrentByID(cityCode)
return weather
return weather, nil
}

View File

@@ -59,6 +59,10 @@ func (widget *Widget) addView() {
}
func (widget *Widget) contentFrom(data *owm.CurrentWeatherData) string {
if len(data.Weather) == 0 {
return " Weather data is unavailable."
}
str := "\n"
descs := []string{}
@@ -90,6 +94,10 @@ func (widget *Widget) contentFrom(data *owm.CurrentWeatherData) string {
func icon(data *owm.CurrentWeatherData) string {
var icon string
if len(data.Weather) == 0 {
return ""
}
switch data.Weather[0].Description {
case "broken clouds":
icon = "☁️"