gal
gal

Reputation: 302

Handling Timeout error

After using timeout:

 status = Timeout::timeout(5) {
  # Something that should be interrupted if it takes too much time...
}

I got this Timeout error:

/Users/galharth/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/net/http.rb:644:in `initialize': execution expired (Timeout::Error)
    from /Users/galharth/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/net/http.rb:644:in `open'
    from /Users/galharth/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/net/http.rb:644:in `block in connect'
    from /Users/galharth/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/timeout.rb:44:in `timeout'
    from /Users/galharth/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/timeout.rb:87:in `timeout'
    from /Users/galharth/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/net/http.rb:644:in `connect'
    from /Users/galharth/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/net/http.rb:637:in `do_start'
    from /Users/galharth/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/net/http.rb:632:in `start'
    from /Users/galharth/.rvm/gems/ruby-1.9.2-p136/gems/mechanize-1.0.0/lib/mechanize.rb:527:in `fetch_page'
    from /Users/galharth/.rvm/gems/ruby-1.9.2-p136/gems/mechanize-1.0.0/lib/mechanize.rb:259:in `get'

What should I do?

Upvotes: 15

Views: 19723

Answers (2)

vikas pal
vikas pal

Reputation: 75

uri = URI.parse("https://example.com")
        # Full control
        http = Net::HTTP.new(uri.host, uri.port)
        http.use_ssl = true
        #http.read_timeout = 500
        request = Net::HTTP::Post.new(uri.request_uri)
        request.set_form_data({"locationcode" => "999", "sublocationcode" => "1"})
        response = http.request(request)
        render :json => response.body

Upvotes: 0

Mladen Jablanović
Mladen Jablanović

Reputation: 44080

Well, that's expected behaviour of Timeout. If the block takes too long, its execution gets terminated and an exception thrown.

You would probably like to catch the exception and handle it appropriately:

require 'timeout'
begin
  status = Timeout::timeout(5) {
    # Something that should be interrupted if it takes too much time...
  }
rescue Timeout::Error
  puts 'That took too long, exiting...'
end

Upvotes: 28

Related Questions