pjb*_*jb3 5 ruby error-handling http
当使用Eventmachine异步执行HTTP请求时,通过使用em-http-request或其他方法,是否可以在errback回调中获取有关错误的信息?像连接错误与超时和无效响应一样?
小智 6
自从问过这个问题已经有一段时间了,但我发现自己今天也在尝试做同样的事情.HttpClient对象上有一个错误getter.这是我使用光纤池的一般方法:
fiber = Fiber.current
@request = EventMachine::HttpRequest.new(url)
@http = @request.get(opts)
@http.errback do
fiber.resume Exception.new("An error occurred in the HTTP request: #{@http.errors}", self)
end
@http.callback do
fiber.resume true
end
result = fiber.yield
raise result if result.kind_of?(Exception)
Run Code Online (Sandbox Code Playgroud)
如果你想区分那些错误,我还有一件事就是检测超时错误.最简单的方法是对请求计时并确定它是否比指定的超时长,但我没有在em-http-request中找到区分错误类型的任何内容.