Topic: error handling with external request

I'm using httparty to make an external request for a json file and then parsing it. How can I handle errors so I can still display the page should the request fail due to a bad uri or the file no longer exists? Just trying to protect my ass for the future. I haven't dealt with error handling, so I have no idea of how to tackle this issue.

Thank you for your help!

def json_api_pull(url)
  JSON.parse HTTParty.get(url).response.body
end
json_api_pull('http://test.com/test.json')['test']
Learning rails, one day at a time.

http://keilmiller.com

Re: error handling with external request

Bump... not a soul out there can point me in the correct direction?

Learning rails, one day at a time.

http://keilmiller.com

Re: error handling with external request

I solved my own issue.

  def json_api_pull(url)
    begin
      JSON.parse HTTParty.get(url).response.body
    rescue
      nil
    end
  end

I then just check if the method returns nil when I use it.

Last edited by tuscanidream (2012-04-08 11:54:17)

Learning rails, one day at a time.

http://keilmiller.com