【问题标题】:Using a splat to catch errors is not working使用 splat 捕获错误不起作用
【发布时间】:2017-01-18 05:01:50
【问题描述】:

我有很多错误需要捕获,因此我将它们全部放入两个数组并创建一个常量来保存它们,但是,当我运行程序时,我收到了异常:

C:/Users/thomas_j_perkins/bin/ruby/tool/sql_tool/whitewidow/lib/imports/constants_and_requires.rb:62:in `<top (required)>': uninitialized constant RestClient::MaxRedirectsReached (NameError)
        from whitewidow.rb:6:in `require_relative'
        from whitewidow.rb:6:in `<main>'

以下是常量的外观:

LOADING_ERRORS = [RestClient::ResourceNotFound, RestClient::InternalServerError, RestClient::RequestTimeout,
                RestClient::Gone, RestClient::SSLCertificateNotVerified, RestClient::Forbidden,
                OpenSSL::SSL::SSLError, Errno::ECONNREFUSED, URI::InvalidURIError, Errno::ECONNRESET,
                Timeout::Error, OpenSSL::SSL::SSLError, Zlib::GzipFile::Error, RestClient::MultipleChoices,
                RestClient::Unauthorized, SocketError, RestClient::BadRequest, RestClient::ServerBrokeConnection,
                RestClient::MaxRedirectsReached]
FATAL_ERRORS = [Mechanize::ResponseCodeError, RestClient::ServiceUnavailable, OpenSSL::SSL::SSLError,
                RestClient::BadGateway]

我是这样使用它们的:

begin
  # Do some cool stuff
rescue *FATAL_ERRORS => e
  puts e
end

--

begin
  # Do some more cool stuff
rescue *LOADING_ERRORS => e
  puts e
end

我做错了什么,我会收到最需要的错误吗?以防万一您需要它,这是错误指定的整个要求文件:

# Built in libraries
require 'rubygems'
require 'bundler/setup'
require 'mechanize'
require 'nokogiri'
require 'rest-client'
require 'timeout'
require 'uri'
require 'fileutils'
require 'yaml'
require 'date'
require 'optparse'
require 'tempfile'
require 'socket'
require 'net/http'

# Created libraries
require_relative '../../lib/modules/format'
require_relative '../../lib/misc/credits'
require_relative '../../lib/misc/legal'
require_relative '../../lib/misc/spider'
require_relative '../../lib/modules/copy'
require_relative '../../lib/modules/site_info'
require_relative '../../lib/modules/expansion/string_expan'

# Modules that need to be included
include Format
include Credits
include Legal
include Whitewidow
include Copy
include SiteInfo

# Constants used throughout the program
=begin
USER_AGENTS = { # Temporary fix for user agents until I can refactor the YAML file
    1 => 'Mozilla/5.0 (compatible; 008/0.83; http://www.80legs.com/webcrawler.html) Gecko/2008032620',
    2 => 'Mozilla/5.0 (compatible; U; ABrowse 0.6; Syllable) AppleWebKit/420+ (KHTML, like Gecko)',
    3 => 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.3pre) Gecko/20100403 Lorentz/3.6.3plugin2pre (.NET CLR 4.0.20506)',
    4 => 'Mozilla/5.0 (compatible; Yahoo! Slurp; http://help.yahoo.com/help/us/ysearch/slurp)',
    5 => 'igdeSpyder (compatible; igde.ru; +http://igde.ru/doc/tech.html)',
    6 => 'larbin_2.6.3 (ltaa_web_crawler@groupes.epfl.ch)',
    7 => 'Mozilla/5.0 (Linux; Android 5.0.2; SAMSUNG SM-T550 Build/LRX22G) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/3.3 Chrome/38.0.2125.102 Safari/537.36',
    8 => 'Dalvik/2.1.0 (Linux; U; Android 6.0.1; Nexus Player Build/MMB29T)',
    9 => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:15.0) Gecko/20100101 Firefox/15.0.1',
    10 => 'Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)',
}
=end
FORMAT = Format::StringFormat.new
PATH = Dir.pwd
VERSION = Whitewidow.version
SEARCH = File.readlines("#{PATH}/lib/lists/search_query.txt").sample
USER_AGENTS = YAML.load_file("#{PATH}/lib/lists/rand-age.yml")
OPTIONS = {}
USER_AGENT = USER_AGENTS[rand(1..10)]
SKIP = %w(/webcache.googleusercontent.com stackoverflow.com github.com)
LOADING_ERRORS = [RestClient::ResourceNotFound, RestClient::InternalServerError, RestClient::RequestTimeout,
                RestClient::Gone, RestClient::SSLCertificateNotVerified, RestClient::Forbidden,
                OpenSSL::SSL::SSLError, Errno::ECONNREFUSED, URI::InvalidURIError, Errno::ECONNRESET,
                Timeout::Error, OpenSSL::SSL::SSLError, Zlib::GzipFile::Error, RestClient::MultipleChoices,
                RestClient::Unauthorized, SocketError, RestClient::BadRequest, RestClient::ServerBrokeConnection,
                RestClient::MaxRedirectsReached]
FATAL_ERRORS = [Mechanize::ResponseCodeError, RestClient::ServiceUnavailable, OpenSSL::SSL::SSLError,
                RestClient::BadGateway]

【问题讨论】:

  • 根据报错需要查看from whitewidow.rb:6:in 'require_relative'这行,需要什么?
  • @BlueSmith 它需要上面的文件以及要求、类、模块和常量

标签: ruby error-handling rest-client rescue splat


【解决方案1】:

我安装了mechanizerest-client

gem install mechanize gem install rest-client

然后我打开了一个 IRB 会话

require mechanize require rest-client

然后测试了您的 FATAL_ERROR 数组,并能够引发错误并使用您的代码处理它。

所以你使用* splat 运算符的方式没有问题。

问题出在您的 LOADING_ERRORS 数组中。

当我尝试对您的 LOADING_ERRORS 数组执行相同操作时,我收到了与您相同的错误消息。

我克隆了rest-client git 存储库并在lib/restclient/exceptions.rb 文件中搜索,似乎没有定义RestClient::MaxRedirectsReached

如果您从数组中删除该异常,代码将起作用。

在存储库中进一步研究后,有一个history.md 文件,它指出:

  • 重定向行为的变化:(#381,#484)
    • 删除RestClient::MaxRedirectsReached以支持正常 ExceptionWithResponse 子类。这使得响应可以在 异常对象为.response,使调用者可以告诉 达到重定向限制时实际发生了什么。
    • 当遵循 HTTP 重定向时,将每个先前响应的列表存储在 响应对象为.history。这样就可以访问 重定向之前的原始响应标头和正文。
    • 始终遵循重定向,无论 HTTP 方法是否 作为符号或字符串传递。在引擎盖下 rest-client 现在标准化 HTTP 请求方法转为小写字符串。

因此,该异常似乎已从 rest-client 库中删除。

您可能希望将其替换为 RestClient::ExceptionWithResponse

【讨论】:

  • 两个注意事项: 1. RestClient::ExceptionWithResponse 异常是rest-client 引发的许多异常的父类,无需一一列举。 2. 令人困惑的是,rest_client 实际上是一个不同的 gem,一个已弃用的 fork。 rubygems.org/gems/rest_clientrubygems.org/gems/rest-client
猜你喜欢
  • 2021-10-06
  • 2019-03-02
  • 1970-01-01
  • 2016-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-11
  • 1970-01-01
相关资源
最近更新 更多