briandowns/openweathermap

Error unmarshalling CurrentWeatherData

Closed this issue · 7 comments

Getting the following error:

json: cannot unmarshal string into Go value of type int

when CurrentByName is called.

It looks like you'll receive this error if you give CurentByName a name which the API cannot find since the endpoint will send back an error object instead of the expected response:

http://api.openweathermap.org/data/2.5/weather?q=idontexist

{"message":"Error: Not found city","cod":"404"}

It might be a good idea to get some better error handling for these cases instead of returning the error from the JSON decoder.

Update:
Stumbled on to this: https://openweathermap.desk.com/customer/portal/questions/9944207-list-of-expected-error-codes

Where it is stated that the error messages, or even the structure of them cannot be relied on. So it looks like the best you could do is check for http.StatusOk and that doesn't appear to be reliable either. :(

Seems like the openweathermap API is having some issues at the moment too, so if you're sure the place should exist, it may just be that. :)

The city seems to exist. For city Kungälv, I am getting error

Fetch error for city Kungälv: json: cannot unmarshal string into Go value of type int

I also get different errors for other cities. I will post them next message.

These are all the errors I am getting.

Fetch error for city Malmö: invalid character 'i' in literal false (expecting 'l')
Fetch error for city Uppsala: EOF
Fetch error for city Västerås: EOF
Fetch error for city Örebro: invalid character 'i' in literal false (expecting 'l')
Fetch error for city Helsingborg: EOF
Fetch error for city Täby: json: cannot unmarshal number into Go value of type openweathermap.CurrentWeatherData
Fetch error for city Växjö: json: cannot unmarshal number into Go value of type openweathermap.CurrentWeatherData
Fetch error for city Halmstad: json: cannot unmarshal number into Go value of type openweathermap.CurrentWeatherData
Fetch error for city Sundsvall: invalid character 'i' in literal false (expecting 'l')
Fetch error for city Luleå: invalid character 'i' in literal false (expecting 'l')
Fetch error for city Trollhättan: invalid character 'i' in literal false (expecting 'l')
Fetch error for city Östersund: invalid character 'i' in literal false (expecting 'l')
Fetch error for city Borlänge: EOF
Fetch error for city Tumba: invalid character 'i' in literal false (expecting 'l')
Fetch error for city Upplands Väsby: EOF
Fetch error for city Skellefteå: EOF
Fetch error for city Lidingö: invalid character 'i' in literal false (expecting 'l')
Fetch error for city Uddevalla: EOF
Fetch error for city Landskrona: invalid character 'i' in literal false (expecting 'l')
Fetch error for city Karlskoga: EOF
Fetch error for city Märsta: EOF
Fetch error for city Kungälv: json: cannot unmarshal string into Go value of type int
Fetch error for city Visby: EOF
Fetch error for city Vänersborg: json: cannot unmarshal number into Go value of type openweathermap.CurrentWeatherData

This demo program was able to get results but only after re-trying a few times (with various errors).

I'm getting intermittent errors when querying the API directly in the browser too:

failed to connect
500 /proxy/data/2.5/weather?q=portsmouth&

Or just no response. I guess we have to wait for it to settle down.

package main

import (
    "fmt"
    "log"

    owm "github.com/briandowns/openweathermap"
)

func main() {
    w, err := owm.NewCurrent("F", "en")
    if err != nil {
        log.Fatal(err)
    }
    if err = w.CurrentByName("Kungälv"); err != nil {
        log.Fatal(err)
    }
    fmt.Printf("%#v\n", w)
}
[benjamin@inara weather]$ go run main.go 
2015/03/13 17:02:36 invalid character 'i' in literal false (expecting 'l')
exit status 1
[benjamin@inara weather]$ go run main.go 
&openweathermap.CurrentWeatherData{GeoPos:openweathermap.Coordinates{Longitude:11.98, Latitude:57.87}, Sys:openweathermap.Sys{Type:1, ID:5387, Message:0.0351, Country:"Sweden", Sunrise:1426224759, Sunset:1426266620}, Base:"cmc stations", Weather:[]openweathermap.Weather{openweathermap.Weather{ID:800, Main:"Clear", Description:"Sky is Clear", Icon:"01d"}}, Main:openweathermap.Main{Temp:44.56, TempMin:42.8, TempMax:47.3, Pressure:1035, Sea_level:0, Grnd_level:0, Humidity:33}, Wind:openweathermap.Wind{Speed:9.94, Deg:70}, Clouds:openweathermap.Clouds{All:0}, Dt:1426264275, ID:2698739, Name:"Kungälv", Cod:200, Unit:"F", Lang:"EN"}

Thanks. Let me know if you have any updates. I will do too.

Well, looks like the OWM API is fine again but I love to hear some ideas on how to update the package to handle stuff like this better. We can adjust to check return codes, return errors depending on which ones they might be, use error struct to unmarshal to if warranted, etc. Thoughts?