Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get info about error when using Eventmachine Http Request

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?

like image 219
pjb3 Avatar asked Jan 09 '10 04:01

pjb3


1 Answers

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.

like image 50
pufferfish Avatar answered Oct 21 '22 12:10

pufferfish