钩子总是在请求后运行 - 也是出错

时间:2015-07-01 13:34:49

标签: mechanize mechanize-ruby

我知道机械化有post_connect_hooks将在检索页面后运行。但是,如果发生异常,例如如果您请求"http://dsjkhbgdfb.comsfg"这样的未知网址,则会运行pre_connect_hooks但不会post_connect_hooks。有没有办法确保始终运行post_connect_hooks(或类似的东西)?

agent = Mechanize.new

agent.pre_connect_hooks << lambda do |request_agent, request|
  puts "increment a counter"
end

agent.post_connect_hooks << lambda do |request_agent, uri, response, response_body|
  puts "decrement a counter"
end

agent.get("http://dsjkhbgdfb.comsfg")
#=> increment a counter
#=> SocketError: getaddrinfo: nodename nor servname provided, or not known

如您所见,“递减计数器”未运行,因为之前发生错误。

1 个答案:

答案 0 :(得分:0)

试试这个,因为它可能只是强制它并忽略错误

virtual int foo(int x) = 0;