Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle exceptions with Ruby Rest-Client

I recently switched from Ruby's Net:HTTP class to rest-client 1.6.7.

I find it a lot easier to form requests, but unlike Net:HTTP request, when rest-client gets anything other than a 200, the request dies. I've tried putting a breakpoint directly after the RestClient.get, and it never gets hit - so I'm doing something wrong.

def get_member_using_card
  resource = "#{@settings_app_uri}api/v1/card/#{self.member_card_num}?token=#{@settings.api_key}"
  response = RestClient.get resource
  if response.code == 200 
    card = JSON.parse(response.body)
    self.customer_id = card['card']['customer_id']
  else
    return 0
  end
end

Which results in this stacktrace:

RestClient::ResourceNotFound - 404 Resource Not Found:
        /Users/tim/.rvm/gems/ruby-1.9.2-p290/gems/rest-client-1.6.7/lib/restclient/abstr
act_response.rb:48:in `return!'
        /Users/tim/.rvm/gems/ruby-1.9.2-p290/gems/rest-client-1.6.7/lib/restclient/reque
st.rb:230:in `process_result'
        /Users/tim/.rvm/gems/ruby-1.9.2-p290/gems/rest-client-1.6.7/lib/restclient/reque
st.rb:178:in `block in transmit'
        /Users/tim/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/net/http.rb:627:in `start'
        /Users/tim/.rvm/gems/ruby-1.9.2-p290/gems/rest-client-1.6.7/lib/restclient/reque
st.rb:172:in `transmit'
        /Users/tim/.rvm/gems/ruby-1.9.2-p290/gems/rest-client-1.6.7/lib/restclient/reque
st.rb:64:in `execute'
        /Users/tim/.rvm/gems/ruby-1.9.2-p290/gems/rest-client-1.6.7/lib/restclient/reque
st.rb:33:in `execute'
        /Users/tim/.rvm/gems/ruby-1.9.2-p290/gems/rest-client-1.6.7/lib/restclient.rb:68
:in `get'

Can someone tell me how to properly evaluate the response code and keep this exception from happening...?

like image 308
tpow Avatar asked Jul 10 '12 22:07

tpow


4 Answers

See heading Exceptions on http://rubydoc.info/gems/rest-client/

  • for results code between 200 and 207 a RestClient::Response will be returned
  • for results code 301, 302 or 307 the redirection will be followed if the request is a get or a head
  • for result code 303 the redirection will be followed and the request transformed into a get
  • for other cases a RestClient::Exception holding the Response will be raised, a specific exception class will be thrown for know error codes

RestClient.get 'http://example.com/resource'
➔ RestClient::ResourceNotFound: RestClient::ResourceNotFound`

begin
  RestClient.get 'http://example.com/resource'
rescue => e
  e.response
end
➔ 404 Resource Not Found | text/html 282 bytes
like image 54
wich Avatar answered Oct 07 '22 02:10

wich


Also in the same documentation @wich pointed to, you can pass a block to RestClient.get such that it will not throw an exception on non-200 response codes:

# Don't raise exceptions but return the response
RestClient.get('http://example.com/resource'){|response, request, result| response }

See the "Result Handling" section from the documentation.

like image 20
Raphael Avatar answered Oct 07 '22 01:10

Raphael


rescue RestClient::ExceptionWithResponse => err
like image 4
Serhiy Nazarov Avatar answered Oct 07 '22 02:10

Serhiy Nazarov


There are several errors that could happen, specific exception types like Errno::EHOSTUNREACH or the more generic ExceptionWithResponse. Check the readme for more info.

like image 1
MegaTux Avatar answered Oct 07 '22 01:10

MegaTux