如何处理Ruby Rest-Client的异常

时间:2022-09-11 14:40:43

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

我最近从Ruby的Net:HTTP类切换到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.

我发现形成请求要容易得多,但与Net:HTTP请求不同,当rest-client获得除200之外的任何东西时,请求就会终止。我尝试在RestClient之后直接放置断点。它不会被击中,所以我做错了什么。

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:

这导致了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...?

有人能告诉我如何正确地评估响应代码并防止这种异常发生吗?

3 个解决方案

#1


49  

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

参见http://rubydoc.info/gems/res -client/上的标题异常

  • for results code between 200 and 207 a RestClient::Response will be returned
  • 对于200到207之间的结果代码,将返回一个RestClient::Response
  • for results code 301, 302 or 307 the redirection will be followed if the request is a get or a head
  • 对于结果代码301、302或307,如果请求是get或head,则会遵循重定向。
  • for result code 303 the redirection will be followed and the request transformed into a get
  • 对于结果代码303,将遵循重定向并将请求转换为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::异常保持响应将被提高,一个特定的异常类将被抛出,以了解错误代码。

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

#2


17  

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:

同样,在@wich指出的同一文档中,您可以向RestClient传递一个块。使它不会对非200个响应码抛出异常:

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

See the "Result Handling" section: http://www.rubydoc.info/gems/rest-client/1.6.7/frames#Result_handling

请参阅“结果处理”部分:http://www.rubydoc.info/gems/res -client/1.6.7/frames#Result_handling

#3


3  

rescue RestClient::ExceptionWithResponse => err

#1


49  

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

参见http://rubydoc.info/gems/res -client/上的标题异常

  • for results code between 200 and 207 a RestClient::Response will be returned
  • 对于200到207之间的结果代码,将返回一个RestClient::Response
  • for results code 301, 302 or 307 the redirection will be followed if the request is a get or a head
  • 对于结果代码301、302或307,如果请求是get或head,则会遵循重定向。
  • for result code 303 the redirection will be followed and the request transformed into a get
  • 对于结果代码303,将遵循重定向并将请求转换为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::异常保持响应将被提高,一个特定的异常类将被抛出,以了解错误代码。

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

#2


17  

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:

同样,在@wich指出的同一文档中,您可以向RestClient传递一个块。使它不会对非200个响应码抛出异常:

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

See the "Result Handling" section: http://www.rubydoc.info/gems/rest-client/1.6.7/frames#Result_handling

请参阅“结果处理”部分:http://www.rubydoc.info/gems/res -client/1.6.7/frames#Result_handling

#3


3  

rescue RestClient::ExceptionWithResponse => err