0

I wrote a rake task that is parsing a json into a hash from an API.

Relevant code is

uri = URI.parse(url)

JSON.parse(uri.open.read)

When I run this rake task, sometimes I get an "end of file reached" error, which I'm assuming is some form of a timeout or the connection closing for whatever reason. Sometimes, the json is parsed without a flaw, other times I get this error.

I'm wondering if there is a different/safer approach, or if there is a way to keep trying to parse until the parse_url completes successfully.

Thanks!

I'm using ruby 1.9.2-p320 and Rails 3.2.6

1 Answers1

2

Solved it by throwing a begin/rescue around JSON.parse and using retry

begin
  return JSON.parse(uri.open.read)
rescue Exception => e
  puts e.message
  puts "retrying.."
  retry
end