3

如果我运行来自 Faraday 的 HTTP 请求,而网络不可用(即在 wifi 环境中或离线工作),那么默认情况下捕获此异常会很好:

/Users/pmu/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/net/http.rb:762:in `initialize': getaddrinfo: nodename or servname provided, or not known (Faraday: :错误::连接失败)
    来自 /Users/pmu/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/net/http.rb:762:in `open'
    来自 /Users/pmu/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/net/http.rb:762:in `block in connect'
    来自 /Users/pmu/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/timeout.rb:54:in `timeout'
    来自 /Users/pmu/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/timeout.rb:99:in `timeout'
    来自 /Users/pmu/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/net/http.rb:762:in `connect'
    来自 /Users/pmu/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/net/http.rb:755:in `do_start'
    来自 /Users/pmu/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/net/http.rb:744:in `start'
    来自 /Users/pmu/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/net/http.rb:1284:in `request'
    来自 /Users/pmu/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/net/http.rb:1026:in `get'
    来自 /Users/pmu/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/faraday-0.8.5/lib/faraday/adapter/net_http.rb:73:in `perform_request '
    来自 /Users/pmu/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/faraday-0.8.5/lib/faraday/adapter/net_http.rb:38:in `call '
    来自 /Users/pmu/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/faraday-0.8.5/lib/faraday/response.rb:8:in `call'
    来自 /Users/pmu/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/faraday-0.8.5/lib/faraday/response/logger.rb:20:in `call '
    来自 /Users/pmu/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/faraday-0.8.5/lib/faraday/request/authorization.rb:36:in `call '
    来自 /Users/pmu/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/faraday-0.8.5/lib/faraday/request/url_encoded.rb:14:in `call '
    来自 /Users/pmu/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/faraday-0.8.5/lib/faraday/request/multipart.rb:13:in `call '
    来自 /Users/pmu/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/faraday-0.8.5/lib/faraday/connection.rb:245:in `run_request'
    来自 /Users/pmu/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/faraday-0.8.5/lib/faraday/connection.rb:98:in `get'
    来自 /Users/pmu/projects/bar/cli/lib/foo/rest_client.rb:32:in `list'

默认情况下,法拉第如何解决这个问题?

4

1 回答 1

6

如果你想让它做一些比完整的堆栈跟踪更好的事情,你可以在你的法拉第设置的基础上做这样的事情:

begin
 @app.call(env)
rescue Faraday::Error::ConnectionFailed => e
  puts "Connection failed: #{e}"
  # Choose what you want to happen, for example in my gem I do
  exit 1
end
于 2013-08-07T13:10:41.423 回答