# Timeout exception open-uri

**URL:** <https://www.ruby-forum.com/t/timeout-exception-open-uri/193534>\
**Category:** Ruby\
**Created:** [August 19, 2010, 11:30am UTC](https://www.ruby-forum.com/t/timeout-exception-open-uri/193534 "2010-08-19T11:30:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![quark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/5e9695/32.png) [@quark](https://www.ruby-forum.com/u/quark)\
**Post date:** [August 19, 2010, 11:30am UTC](https://www.ruby-forum.com/t/timeout-exception-open-uri/193534/1 "2010-08-19T11:30:38Z")

</div>

Hello people… I’m kinda new to Ruby, so probably this is just a stupid  
question…

I’m crawling some websites to get some information and I’m getting this  
error:

/usr/lib/ruby/1.8/net/http.rb:560:in `initialize': Connection timed out - connect(2) (Errno::ETIMEDOUT) from /usr/lib/ruby/1.8/net/http.rb:560:in`open’  
from /usr/lib/ruby/1.8/net/http.rb:560:in `connect' from /usr/lib/ruby/1.8/timeout.rb:53:in`timeout’  
from /usr/lib/ruby/1.8/timeout.rb:93:in `timeout' from /usr/lib/ruby/1.8/net/http.rb:560:in`connect’  
from /usr/lib/ruby/1.8/net/http.rb:553:in `do_start' from /usr/lib/ruby/1.8/net/http.rb:542:in`start’  
from /usr/lib/ruby/1.8/open-uri.rb:242:in `open_http' from /usr/lib/ruby/1.8/open-uri.rb:616:in`buffer\_open’  
from /usr/lib/ruby/1.8/open-uri.rb:164:in `open_loop' from /usr/lib/ruby/1.8/open-uri.rb:162:in`catch’  
from /usr/lib/ruby/1.8/open-uri.rb:162:in `open_loop' from /usr/lib/ruby/1.8/open-uri.rb:132:in`open\_uri’  
from /usr/lib/ruby/1.8/open-uri.rb:518:in `open' from /usr/lib/ruby/1.8/open-uri.rb:30:in`open’  
from ./folder/file.rb:111:in `get_stuff' from /usr/lib/ruby/1.8/timeout.rb:62:in`timeout’  
from ./folder/file.rb:109:in `get_stuff' from ./folder/file.rb:88:in`each’  
from ./folder/file.rb:88:in `get_stuff' from ./folder/file.rb:77:in`get\_stuff\_subcategories’  
from ./folder/file.rb:74:in `each' from ./folder/file.rb:74:in`get\_stuff\_subcategories’  
from ./folder/file.rb:72:in `each' from ./folder/file.rb:72:in`get\_stuff\_subcategories’  
from ./folder/site.rb:51:in `run’  
from /home/user/NetBeansProjects/project/lib/main.rb:30

This only happen sometimes, and happen specially when my internet  
connection is slow. So I assume is because of that (a timeout, or  
something like that).

Complete code:

begin  
Timeout::timeout(40){  
doc = Nokogiri::HTML(open(begin\_url))  
#do some stuff with doc variable  
}  
rescue Net::HTTPBadResponse =\> e  
log.error ‘Fail!’  
retries\_torr -= 1  
if retries\_torr \> 0  
retry  
end  
rescue Mechanize::ResponseCodeError =\> ex  
log.error ‘Fail’  
if retries\_torr \> 0  
retry  
end  
rescue Timeout::Error =\> e  
log.error ‘Fail’  
retries -= 1  
if retries \> 0  
retry  
else  
log.error ‘Fail opening the page. Check the internet connection.’  
end  
end

The exception is generated in this line:

doc = Nokogiri::HTML(open(begin\_url))

The thing is, I’m trying to catch that exception and I’m not getting  
success with that task. I tried:  
rescue Timeout::Error

But didn’t work.  
Can any of you give me some help here?

Thanks,

Luis

---

<div class="post-metadata">

**Author:** ![quark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/5e9695/32.png) [@quark](https://www.ruby-forum.com/u/quark)\
**Post date:** [August 19, 2010, 11:44am UTC](https://www.ruby-forum.com/t/timeout-exception-open-uri/193534/2 "2010-08-19T11:44:49Z")

</div>

Luis Some wrote:

> The thing is, I’m trying to catch that exception and I’m not getting  
> success with that task. I tried:  
> rescue Timeout::Error

That’s not the exception you’re getting (read the error message  
carefully). It’s:

rescue Errno::ETIMEDOUT

Or better,

rescue SystemCallError

which is the superclass of all the Errno::\* errors. These are errors  
generated by the operating system itself, and this one is occuring  
before your 40 second timeout has expired.

---

<div class="post-metadata">

**Author:** ![quark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/5e9695/32.png) [@quark](https://www.ruby-forum.com/u/quark)\
**Post date:** [August 19, 2010, 12:22pm UTC](https://www.ruby-forum.com/t/timeout-exception-open-uri/193534/3 "2010-08-19T12:22:49Z")

</div>

Brian C. wrote:

> Luis Some wrote:
> 
> > The thing is, I’m trying to catch that exception and I’m not getting  
> > success with that task. I tried:  
> > rescue Timeout::Error
> 
> That’s not the exception you’re getting (read the error message  
> carefully). It’s:
> 
> rescue Errno::ETIMEDOUT
> 
> Or better,
> 
> rescue SystemCallError
> 
> which is the superclass of all the Errno::\* errors. These are errors  
> generated by the operating system itself, and this one is occuring  
> before your 40 second timeout has expired.

Hey Brian

Thanks for your reply.

I’m gonna do some tests with that options you gave me.  
And yes, was a stupid question…

If I get other errors, I’ll let you know.

Thanks,

Luis
