简体   繁体   English

红宝石捕获连接超时错误

[英]ruby catch connection timeout error

I am using Mechanize in Ruby to post forms to a site to obtain data. 我在Ruby中使用Mechanize将表单发布到站点以获取数据。 Occasionally, I will get the error 有时,我会收到错误消息

too many connection resets (due to Operation timed out - Errno::ETIMEDOUT) after 13 requests on 34234234234242, last used 20.518373 seconds ago

This will crash the server. 这将使服务器崩溃。 I would like to catch this error and handle it(by retrying later on). 我想捕获此错误并进行处理(稍后重试)。

I tried 我试过了

 begin 
    postForm(form)
  rescue Errno::ETIMEDOUT
    puts "=====>TimeOut ERROR!:"  
  end

But it is not catching the error. 但是它没有捕获错误。 What am I missing? 我想念什么?

Thanks. 谢谢。

It looks like Mechanize caught the Errno::ETIMEDOUT error and threw a new exception containing the error message. 看起来Mechanize捕获了Errno::ETIMEDOUT错误,并引发了一个包含错误消息的新异常。 Therefore, the exception class name is different, and can't be caught with Errno::ETIMEDOUT . 因此,异常类名是不同的,并且不能用Errno::ETIMEDOUT

To determine the type of error, try this: 要确定错误的类型,请尝试以下操作:

begin 
  postForm(form)
rescue => e
  puts e.class  
end

That will give you the exception class name, and you can update the rescue clause. 这将为您提供异常类名称,并且您可以更新救援子句。

If you read the entire error message, you see that you failed because of a 'too many connection resets' : The timeout is only what caused the connection to reset 13 times, and has already been rescued. 如果您阅读了整个错误消息,则会看到由于“太多的连接重置”而​​导致失败:超时只是导致连接重置13次的原因,并且已经被挽救。

So if you read the source of Net-Http-persistent >here< , you will see that the error you need to catch is : 因此,如果您阅读Net-Http-persistent > here <的源代码,您将看到需要捕获的错误是:

Net::HTTP::Persistent::Error

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM