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

41

我最近从 Ruby 的 Net:HTTP 类切换到 rest-client 1.6.7。

我发现使用 rest-client 更容易构建请求,但与 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

这导致产生以下堆栈跟踪:

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'

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

6个回答

61

请参见http://rubydoc.info/gems/rest-client/上的Exceptions标题。

  • 对于返回码在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

26
同样在@wich指向的文档中,您可以传递一个块到RestClient.get中,这样它就不会在非200响应代码上抛出异常。
# Don't raise exceptions but return the response
RestClient.get('http://example.com/resource'){|response, request, result| response }

请查看文档中的“结果处理”部分from the documentation

5
rescue RestClient::ExceptionWithResponse => err

1

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


1

我认为处理API客户端异常的最佳方式是获取API端点抛出的原始错误消息。以下是使用RestClient处理该问题的示例代码:

require 'json'

def get_call
    begin
        standard_response = {body: nil, success: false, message: ''}

        response = RestClient.get('https://example.com/api/v1/xx', headers={'Authorization' => 'AbcDef xxx'})

        standard_response[:success] = true
        standard_response[:body] = JSON.parse(response.body)
    rescue RestClient::ExceptionWithResponse => e
        http_body = JSON.parse(e.http_body) # This is the original response from the API endpoint. e.g. {'message': 'Reason for the failure'}
        meaningful_error_message = http_body['message'].nil? ? e.message : http_body['message'] # if {'message': 'error message'} is the format of your API
        standard_response[:message] = meaningful_error_message
    end
    
    standard_response
end

-2

在 REST 客户端中处理异常的优美方式。

如需更多信息,请查看 rest-client#response-callbacks-error-handling

RestClient.get('http://example.com/resource') { |response, request, result, &block|
  case response.code
  when 200
    p "It worked !"
    response
  when 423
    raise SomeCustomExceptionIfYouWant
  else
    response.return!(&block)
  end
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接