簡體   English   中英

如何處理SocketError - getaddrinfo:提供nodename或servname,或者不知道[ruby]

[英]How to handle SocketError - getaddrinfo: nodename nor servname provided, or not known [ruby]

所以,我正在創建一個檢查工具運行狀況檢查的腳本,腳本所做的第一件事是檢查它是否真的可以實際到達指定的URL(通過檢查HTTP代碼)。 我有一個問題,如果URL無效,它會導致SocketError - getaddrinfo: nodename nor servname provided, or not known哪個引發Internal Server error (500) (這是可以理解的)..

但不是這樣,我想處理該錯誤,而是向用戶返回自定義“錯誤”。

在發布此內容之前,我一直在谷歌搜索解決方案已經有一段時間了,我找不到任何對我的問題有用的東西。 :/

我的腳本使用的是Sinatra,這是與此問題有關的主要部分:

def check_status(uri_str, is_verbose, limit = 10)
    response = RestClient.get "#{uri_str}" # This is line 35

    case response.code
        when 200...300
            @@services.each do |service|
                if service.is_matched?("#{uri_str}")
        @@tool_name = service.class.name.split('::').last.downcase
                    puts "This is the URL inside the 'if 
                    @@result = service.check_tool_specific("#{uri_str}", "#{verbose(is_verbose)}")
      end
            end

            if @@result.nil?
                return 'No results, something went wrong!'
            end

    return "{\"#{@@tool_name}\":{\"timestamp\":\"#{Time.now.strftime('%Y-%m-%d %T')}\",\"results\":#{@@result.to_json}}}"

  when Net::HTTPRedirection then
    puts "Response code in Net::HTTPRedirection: #{response.code}"
            location = response['location']
            check_status(location, limit - 1)
        else
            puts "It doesn't work!"
            puts "#{response.code}"
  end
end

目前,如果URL無效,這是我在控制台中收到的錯誤:

2016-10-11 15:46:32 - SocketError - getaddrinfo: nodename nor servname provided, or not known:
    /Users/farahfa/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/http.rb:879:in `initialize'
    /Users/farahfa/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/http.rb:879:in `open'
    /Users/farahfa/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/http.rb:879:in `block in connect'
    /Users/farahfa/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/timeout.rb:74:in `timeout'
    /Users/farahfa/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/http.rb:878:in `connect'
    /Users/farahfa/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/http.rb:863:in `do_start'
    /Users/farahfa/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/http.rb:852:in `start'
    /Users/farahfa/.rvm/gems/ruby-2.2.2@global/gems/rest-client-2.0.0/lib/restclient/request.rb:766:in `transmit'
    /Users/farahfa/.rvm/gems/ruby-2.2.2@global/gems/rest-client-2.0.0/lib/restclient/request.rb:215:in `execute'
    /Users/farahfa/.rvm/gems/ruby-2.2.2@global/gems/rest-client-2.0.0/lib/restclient/request.rb:52:in `execute'
    /Users/farahfa/.rvm/gems/ruby-2.2.2@global/gems/rest-client-2.0.0/lib/restclient.rb:67:in `get'
    /Users/farahfa/temp/ruby-devtools-hc/devtools-hc.rb:35:in `check_status'
    /Users/farahfa/temp/ruby-devtools-hc/devtools-hc.rb:22:in `block in <class:DevMonit>'
    /Users/farahfa/.rvm/gems/ruby-2.2.2@global/gems/sinatra-1.4.7/lib/sinatra/base.rb:1611:in `call'
    /Users/farahfa/.rvm/gems/ruby-2.2.2@global/gems/sinatra-1.4.7/lib/sinatra/base.rb:1611:in `block in compile!'
    /Users/farahfa/.rvm/gems/ruby-2.2.2@global/gems/sinatra-1.4.7/lib/sinatra/base.rb:975:in `[]'
    /Users/farahfa/.rvm/gems/ruby-2.2.2@global/gems/sinatra-1.4.7/lib/sinatra/base.rb:975:in `block (3 levels) in route!'
    /Users/farahfa/.rvm/gems/ruby-2.2.2@global/gems/sinatra-1.4.7/lib/sinatra/base.rb:994:in `route_eval'
    /Users/farahfa/.rvm/gems/ruby-2.2.2@global/gems/sinatra-1.4.7/lib/sinatra/base.rb:975:in `block (2 levels) in route!'
    /Users/farahfa/.rvm/gems/ruby-2.2.2@global/gems/sinatra-1.4.7/lib/sinatra/base.rb:1015:in `block in process_route'
    /Users/farahfa/.rvm/gems/ruby-2.2.2@global/gems/sinatra-1.4.7/lib/sinatra/base.rb:1013:in `catch'
    /Users/farahfa/.rvm/gems/ruby-2.2.2@global/gems/sinatra-1.4.7/lib/sinatra/base.rb:1013:in `process_route'
    /Users/farahfa/.rvm/gems/ruby-2.2.2@global/gems/sinatra-1.4.7/lib/sinatra/base.rb:973:in `block in route!'
    /Users/farahfa/.rvm/gems/ruby-2.2.2@global/gems/sinatra-1.4.7/lib/sinatra/base.rb:972:in `each'
    /Users/farahfa/.rvm/gems/ruby-2.2.2@global/gems/sinatra-1.4.7/lib/sinatra/base.rb:972:in `route!'
    /Users/farahfa/.rvm/gems/ruby-2.2.2@global/gems/sinatra-1.4.7/lib/sinatra/base.rb:1085:in `block in dispatch!'
    /Users/farahfa/.rvm/gems/ruby-2.2.2@global/gems/sinatra-1.4.7/lib/sinatra/base.rb:1067:in `block in invoke'
    /Users/farahfa/.rvm/gems/ruby-2.2.2@global/gems/sinatra-1.4.7/lib/sinatra/base.rb:1067:in `catch'
    /Users/farahfa/.rvm/gems/ruby-2.2.2@global/gems/sinatra-1.4.7/lib/sinatra/base.rb:1067:in `invoke'
    /Users/farahfa/.rvm/gems/ruby-2.2.2@global/gems/sinatra-1.4.7/lib/sinatra/base.rb:1082:in `dispatch!'
    /Users/farahfa/.rvm/gems/ruby-2.2.2@global/gems/sinatra-1.4.7/lib/sinatra/base.rb:907:in `block in call!'
    /Users/farahfa/.rvm/gems/ruby-2.2.2@global/gems/sinatra-1.4.7/lib/sinatra/base.rb:1067:in `block in invoke'
    /Users/farahfa/.rvm/gems/ruby-2.2.2@global/gems/sinatra-1.4.7/lib/sinatra/base.rb:1067:in `catch'
    /Users/farahfa/.rvm/gems/ruby-2.2.2@global/gems/sinatra-1.4.7/lib/sinatra/base.rb:1067:in `invoke'
    /Users/farahfa/.rvm/gems/ruby-2.2.2@global/gems/sinatra-1.4.7/lib/sinatra/base.rb:907:in `call!'
    /Users/farahfa/.rvm/gems/ruby-2.2.2@global/gems/sinatra-1.4.7/lib/sinatra/base.rb:895:in `call'
    /Users/farahfa/.rvm/gems/ruby-2.2.2@global/gems/rack-protection-1.5.3/lib/rack/protection/xss_header.rb:18:in `call'
    /Users/farahfa/.rvm/gems/ruby-2.2.2@global/gems/rack-protection-1.5.3/lib/rack/protection/path_traversal.rb:16:in `call'
    /Users/farahfa/.rvm/gems/ruby-2.2.2@global/gems/rack-protection-1.5.3/lib/rack/protection/json_csrf.rb:18:in `call'
    /Users/farahfa/.rvm/gems/ruby-2.2.2@global/gems/rack-protection-1.5.3/lib/rack/protection/base.rb:49:in `call'
    /Users/farahfa/.rvm/gems/ruby-2.2.2@global/gems/rack-protection-1.5.3/lib/rack/protection/base.rb:49:in `call'
    /Users/farahfa/.rvm/gems/ruby-2.2.2@global/gems/rack-protection-1.5.3/lib/rack/protection/frame_options.rb:31:in `call'
    /Users/farahfa/.rvm/gems/ruby-2.2.2@global/gems/rack-1.6.4/lib/rack/nulllogger.rb:9:in `call'
    /Users/farahfa/.rvm/gems/ruby-2.2.2@global/gems/rack-1.6.4/lib/rack/head.rb:13:in `call'
    /Users/farahfa/.rvm/gems/ruby-2.2.2@global/gems/sinatra-1.4.7/lib/sinatra/show_exceptions.rb:25:in `call'
    /Users/farahfa/.rvm/gems/ruby-2.2.2@global/gems/sinatra-1.4.7/lib/sinatra/base.rb:182:in `call'
    /Users/farahfa/.rvm/gems/ruby-2.2.2@global/gems/sinatra-1.4.7/lib/sinatra/base.rb:2013:in `call'
    /Users/farahfa/.rvm/gems/ruby-2.2.2@global/gems/sinatra-1.4.7/lib/sinatra/base.rb:1487:in `block in call'
    /Users/farahfa/.rvm/gems/ruby-2.2.2@global/gems/sinatra-1.4.7/lib/sinatra/base.rb:1787:in `synchronize'
    /Users/farahfa/.rvm/gems/ruby-2.2.2@global/gems/sinatra-1.4.7/lib/sinatra/base.rb:1487:in `call'
    /Users/farahfa/.rvm/gems/ruby-2.2.2@global/gems/rack-1.6.4/lib/rack/tempfile_reaper.rb:15:in `call'
    /Users/farahfa/.rvm/gems/ruby-2.2.2@global/gems/rack-1.6.4/lib/rack/lint.rb:49:in `_call'
    /Users/farahfa/.rvm/gems/ruby-2.2.2@global/gems/rack-1.6.4/lib/rack/lint.rb:37:in `call'
    /Users/farahfa/.rvm/gems/ruby-2.2.2@global/gems/rack-1.6.4/lib/rack/showexceptions.rb:24:in `call'
    /Users/farahfa/.rvm/gems/ruby-2.2.2@global/gems/rack-1.6.4/lib/rack/commonlogger.rb:33:in `call'
    /Users/farahfa/.rvm/gems/ruby-2.2.2@global/gems/sinatra-1.4.7/lib/sinatra/base.rb:219:in `call'
    /Users/farahfa/.rvm/gems/ruby-2.2.2@global/gems/rack-1.6.4/lib/rack/chunked.rb:54:in `call'
    /Users/farahfa/.rvm/gems/ruby-2.2.2@global/gems/rack-1.6.4/lib/rack/content_length.rb:15:in `call'
    /Users/farahfa/.rvm/gems/ruby-2.2.2@global/gems/rack-1.6.4/lib/rack/handler/webrick.rb:88:in `service'
    /Users/farahfa/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service'
    /Users/farahfa/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run'
    /Users/farahfa/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'

注意 :第22行只是: check_status(url, is_verbose)調用該函數。

非常感謝任何幫助和/或指導!

由於您使用的是RestClient的第2版,因此推薦的方法是在方法end之前使用e.response添加rescue RestClient::ExceptionWithResponse => e #do。

您可以在此處查看RestClient可以引發的所有異常的完整列表: https//github.com/rest-client/rest-client#exceptions-see-httpwwww3orgprotocolsrfc2616rfc2616-sec10html

我建議嘗試抓住他們:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM