2016-09-10 46 views
0

我有很多錯誤,我需要趕上,所以我把它們全部放到兩個數組中,並做了一個常數來保存它們,但是,當我運行程序時,我收到例外:使用圖示來捕捉錯誤不起作用

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 

我做錯了什麼地方我會收到最高要求的錯誤?萬一你需要它,這裏的整個要求的文件,錯誤被指定:我安裝mechanizerest-client

# 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 ([email protected])', 
    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] 
+0

來取代它根據的錯誤,你需要檢查從whitewidow.rb行':6:「require_relative'',這是什麼要求? –

+0

@BlueSmith它需要上面的文件與要求,類,模塊和常量 –

回答

1

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文件,它規定:

  • 更改redirection(重定向)行爲:(#381,#484)
    • 贊成刪除RestClient::MaxRedirectsReached正常的 ExceptionWithResponse子類。這使得 可以通過 將異常對象作爲.response訪問,從而使呼叫者可以告知 達到重定向限制時實際發生了什麼。
    • 當遵循HTTP重定向時,將每個先前響應的列表存儲在 響應對象中作爲.history。這使得可以在遵循重定向之前訪問 原始響應標頭和主體。
    • 無論HTTP方法是否作爲符號或字符串傳遞 ,都一致地遵循重定向。在rest-client下,現在將 HTTP請求方法標準化爲小寫字符串。

因此它似乎是例外,已經從rest-client庫中刪除。

您可能希望與RestClient::ExceptionWithResponse

+1

嗯,以及我會被詛咒,它的工作。謝謝! –

+1

兩個注意事項: 1. RestClient :: ExceptionWithResponse異常是許多「rest-client」引發的異常的父類,不需要枚舉所有這些異常。 2.令人困惑的是,'rest_client'實際上是一個不同的gem,一個不推薦的分支。 https://rubygems.org/gems/rest_client https://rubygems.org/gems/rest-client – alberge