Как мне поймать это исключение в Ruby? - PullRequest
3 голосов
/ 01 августа 2011

Я новичок в рубине, так что терпите меня.

Я использую гемы selenium-webdriver и rb-appscript, чтобы сделать некоторую веб-операцию.Навигация по веб-сайтам, по-видимому, обусловлена ​​объектом Net :: Http, который имеет метод rbuf_fill.

Запуск следующего кода:

sites = File.open("sites.txt", "r") if File::exists?( "sites.txt" )

    if sites != nil
       while (line = sites.gets)

              driver.switch_to.default_content

          begin
                 driver.navigate.to line

          rescue Exception
                 line = line.split.join("\n")
                 puts line + " caused a timeout."
          end

       end

...

Выдает эту ошибку:

/opt/local/lib/ruby1.9/1.9.1/net/protocol.rb:140:in `rescue in rbuf_fill': Timeout::Error (Timeout::Error)
from /opt/local/lib/ruby1.9/1.9.1/net/protocol.rb:134:in `rbuf_fill'
from /opt/local/lib/ruby1.9/1.9.1/net/protocol.rb:116:in `readuntil'
from /opt/local/lib/ruby1.9/1.9.1/net/protocol.rb:126:in `readline'
from /opt/local/lib/ruby1.9/1.9.1/net/http.rb:2219:in `read_status_line'
from /opt/local/lib/ruby1.9/1.9.1/net/http.rb:2208:in `read_new'
from /opt/local/lib/ruby1.9/1.9.1/net/http.rb:1191:in `transport_request'
from /opt/local/lib/ruby1.9/1.9.1/net/http.rb:1177:in `request'
from /opt/local/lib/ruby1.9/1.9.1/net/http.rb:1170:in `block in request'
from /opt/local/lib/ruby1.9/1.9.1/net/http.rb:627:in `start'
from /opt/local/lib/ruby1.9/1.9.1/net/http.rb:1168:in `request'
from /opt/local/lib/ruby1.9/gems/1.9.1/gems/selenium-webdriver-2.2.0/lib/selenium/webdriver/remote/http/default.rb:73:in `response_for'
from /opt/local/lib/ruby1.9/gems/1.9.1/gems/selenium-webdriver-2.2.0/lib/selenium/webdriver/remote/http/default.rb:41:in `request'
from /opt/local/lib/ruby1.9/gems/1.9.1/gems/selenium-webdriver-2.2.0/lib/selenium/webdriver/remote/http/common.rb:34:in `call'
from /opt/local/lib/ruby1.9/gems/1.9.1/gems/selenium-webdriver-2.2.0/lib/selenium/webdriver/remote/bridge.rb:406:in `raw_execute'
from /opt/local/lib/ruby1.9/gems/1.9.1/gems/selenium-webdriver-2.2.0/lib/selenium/webdriver/remote/bridge.rb:384:in `execute'
from /opt/local/lib/ruby1.9/gems/1.9.1/gems/selenium-webdriver-2.2.0/lib/selenium/webdriver/remote/bridge.rb:171:in `switchToDefaultContent'
from /opt/local/lib/ruby1.9/gems/1.9.1/gems/selenium-webdriver-2.2.0/lib/selenium/webdriver/common/target_locator.rb:68:in `default_content'
from auto.rb:25:in `<main>'

Понятия не имею, почему я не могу поймать это исключение.Использование rescue Exception должно перехватить все, но, как вы можете видеть, мой скрипт все еще падает.

Я также нашел источники, которые говорят, что вы должны явно перехватить время ожидания, поэтому я также попытался:

rescue Timeout::Error

без всякой удачи.

Любая помощь очень ценится на этом.

Ruby версия: ruby ​​1.9.2p290 (2011-07-09, редакция 32553)

ОС: MacOS Snow Leopard 10.6.8, 64-битная

Версия Selenium Webdriver: 2.2.0

1 Ответ

2 голосов
/ 01 августа 2011

Файл 'timeout.rb' в стандартной библиотеке Ruby определяет:

module Timeout
  # Raised by Timeout#timeout when the block times out.
  class Error < RuntimeError

Так что вам нужно rescue - это не Timeout::Exception, а скорее Timeout::Error или более обобщенно RuntimeError.Тогда это должно работать.

...