如何使用 Ruby Rest-Client 处理异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11423068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 05:16:52  来源:igfitidea点击:

How to handle exceptions with Ruby Rest-Client

rubyrest-client

提问by tpow

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.get 之后直接放置一个断点,但它永远不会被击中 - 所以我做错了什么。

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...?

有人可以告诉我如何正确评估响应代码并防止发生此异常......?

回答by wich

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

看标题异常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
  • 对于 200 到 207 之间的结果代码,将返回 RestClient::Response
  • 对于结果代码 301、302 或 307,如果请求是 get 或 head,将遵循重定向
  • 对于结果代码 303,将遵循重定向并将请求转换为 get
  • 对于其他情况,将引发 RestClient::Exception 持有响应,将抛出特定异常类以获取已知错误代码


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

回答by Raphael

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.get,这样它就不会在非 200 响应代码上引发异常:

# 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.

请参阅文档中的“结果处理”部分。

回答by Serhiy Nazarov

rescue RestClient::ExceptionWithResponse => err

回答by MegaTux

There are several errors that could happen, specific exception types like Errno::EHOSTUNREACHor the more generic ExceptionWithResponse. Check the readmefor more info.

可能会发生多种错误,特定的异常类型,如Errno::EHOSTUNREACH或更通用的 ExceptionWithResponse。查看自述文件以获取更多信息。