coderhs / ruby_open_weather_map

A ruby wrapper for open weather map
MIT License
79 stars 52 forks source link

better error handling #21

Open rpbaltazar opened 9 years ago

rpbaltazar commented 9 years ago

JSON::ParserError: 757: unexpected token at '500 /proxy/data/2.5/forecast?q=%2ccoimbra%2c+pt& ' /app/vendor/bundle/ruby/2.0.0/gems/json-1.8.2/lib/json/common.rb:155:in parse' /app/vendor/bundle/ruby/2.0.0/gems/json-1.8.2/lib/json/common.rb:155:inparse' /app/vendor/bundle/ruby/2.0.0/gems/open-weather-0.11.0/lib/open_weather/base.rb:40:in parse_response' /app/vendor/bundle/ruby/2.0.0/gems/open-weather-0.11.0/lib/open_weather/base.rb:17:inretrive' /app/vendor/bundle/ruby/2.0.0/gems/open-weather-0.11.0/lib/open_weather/api.rb:7:in `city'

coderhs commented 9 years ago

Kindly provide more details about how you reached this issue. Would be great if you could share the piece of the code so that we can reproduce this error.

rpbaltazar commented 9 years ago

    def forecast(location)
      location_forecast = OpenWeather::Forecast.city(location)
      if location_forecast['cod'] == '200'
        forecast_to_use = get_closest_forecast location_forecast['list']
        build_weather_string location, forecast_to_use
      else
        build_error_message location
      end
    end

Nothing special. I believe it might have to do with quotas and the openweather server answering with an error code and not parseable JSON. If you take a closer look at the error that I pasted before,

JSON::ParserError: 757: unexpected token at '500

Seams that the server is answering you with 500 (internal server error) and then you try to parse it which causes an exception.