捕捉机械化 404 =>网络::HTTPNotFound

发布于 2024-11-05 10:02:39 字数 618 浏览 0 评论 0原文

我编写了处理 url 获取的简单函数:

def tender_page_get url, agent
  sleep(rand(6)+2)
  begin
    return agent.get(url).parser
  rescue Errno::ETIMEDOUT, Timeout::Error, Net::HTTPNotFound
    EYE.debug "--winter sleep #{url}"
    puts "-x-#{url}"
    sleep(300)
    tender_page_get url, agent
  rescue => e
    puts "-x-#{url}"
    EYE.debug "--unknown exception"
    EYE.debug "#{url} #{e.inspect}"
  end
end

问题是,即使我在第一个救援块中捕获 Net::HTTPNotFound ,我仍然在日志记录中看到如下内容:

--unknown exception
{url} 404 => Net::HTTPNotFound

这意味着这第二个救援块捕获了异常。原因可能是什么?

I wrote simple function which handles fetching of the url:

def tender_page_get url, agent
  sleep(rand(6)+2)
  begin
    return agent.get(url).parser
  rescue Errno::ETIMEDOUT, Timeout::Error, Net::HTTPNotFound
    EYE.debug "--winter sleep #{url}"
    puts "-x-#{url}"
    sleep(300)
    tender_page_get url, agent
  rescue => e
    puts "-x-#{url}"
    EYE.debug "--unknown exception"
    EYE.debug "#{url} #{e.inspect}"
  end
end

The problem is, even though I am catching Net::HTTPNotFound in my first rescue block, I still see in my log records like:

--unknown exception
{url} 404 => Net::HTTPNotFound

which means that this exception was caught by the second rescue block. What could be the reason for that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

以可爱出名 2024-11-12 10:02:39

Mechanize 针对 404 引发 Mechanize::ResponseCodeError,而不是 Net::HTTPNotFound。 Mechanize::ResponseCodeError 上的 to_s 如下所示:

def to_s
  "#{response_code} => #{Net::HTTPResponse::CODE_TO_OBJ[response_code]}"
end

This returns '404 => Net::HTTPNotFound' 这使得它看起来像是引发的异常。

Mechanize raises a Mechanize::ResponseCodeError for a 404 and not a Net::HTTPNotFound. The to_s on Mechanize::ResponseCodeError looks like this:

def to_s
  "#{response_code} => #{Net::HTTPResponse::CODE_TO_OBJ[response_code]}"
end

This returns '404 => Net::HTTPNotFound' which makes it look like this is the exception being raised.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文