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:
committed by
Chris Cummer
parent
01aab4cc53
commit
204e3b4032
@@ -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
|
||||
}
|
||||
|
||||
@@ -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 = "☁️"
|
||||
|
||||
Reference in New Issue
Block a user