When using Eventmachine to asynchronously execute an HTTP request, either by using em-http-request or some other means, is it possible to get information about the error in the errback callback? Like connection error vs. timeout vs. invalid response?
It's been a while since this one was asked, but I found myself trying to do the same thing today. There's an errors getter on the HttpClient object. Here's my general approach, using a fiber pool:
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)
One thing I haven't figured out how to do is detect timeout errors, if you want to differentiate those from anything else. The simplest approach would be to time the request and determine if it's longer than the timeout specified, but I haven't found anything in em-http-request that distinguishes error types.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With