Ruby 最完整的 Ruby 网络异常列表?

iwinux · 2012年03月05日 · 最后由 vkill 回复于 2012年03月05日 · 6048 次阅读

一个网络连接(通常是 HTTP)从建立到返回的过程中有可能会出现很多种异常情况,比如 DNS 解析错误,比如服务器超时,比如 404……

我的问题:

  1. 在 Ruby 中有哪些异常是跟网络连接有关的?我 Google 到这篇文章,作者列出的是 SocketError, Timeout::Error, ActiveResource::TimeoutError, Errno::ECONNREFUSED, Errno::EHOSTDOWN, Errno::EHOSTUNREACH,显然这并不是完整的列表……
  2. 下面两种写法哪种更好?

捕获所有异常

require 'net/http'

begin
  content = Net::HTTP.get_response(URI('http://ruby-china.org')).body
rescue Exception
  content = ''
end

捕获每一个指定的异常(不完全的列表)

require 'net/http'

begin
  content = Net::HTTP.get_response(URI('http://ruby-china.org')).body
rescue SocketError, Errno::ETIMEDOUT, Errno::ECONNREFUSED, 
       Errno::EHOSTDOWN, Errno::EHOSTUNREACH

  content = ''
end

我觉得只捕获 timeout 和 其他异常就好了吧。另外 404 500 等不算 “网络异常” 吧?

#1 楼 @vkill 除了 404 500,“其他异常” 包括哪些异常呢?我要怎么确定自己没有漏掉一部分呢……

@iwinux 这个得看你用的哪个 gem 吧?gem 中是否很详细的定义这些 error

比如 net/http net/http.rb 里 require 了 net/protocol 和 openssl 那么就可以在这两个文件里查找定义的 error 类了 如

#http.rb 里的
class HTTPBadResponse < StandardError; end
class HTTPHeaderSyntaxError < StandardError; end

#protocol.rb 里的
class ProtocolError          < StandardError; end
class ProtoSyntaxError       < ProtocolError; end
class ProtoFatalError        < ProtocolError; end
class ProtoUnknownError      < ProtocolError; end
class ProtoServerError       < ProtocolError; end
class ProtoAuthError         < ProtocolError; end
class ProtoCommandError      < ProtocolError; end
class ProtoRetriableError    < ProtocolError; end
ProtocRetryError = ProtoRetriableError

当然还有 openssl 的,自己查下了

@iwinux 另外 404 500 是 response 错误哈,也可以看官方文档

HTTPResponse
    HTTPUnknownResponse
    HTTPInformation                    # 1xx
        HTTPContinue                       # 100
        HTTPSwitchProtocol                 # 101
    HTTPSuccess                        # 2xx
        HTTPOK                             # 200
        HTTPCreated                        # 201
        HTTPAccepted                       # 202
        HTTPNonAuthoritativeInformation    # 203
        HTTPNoContent                      # 204
        HTTPResetContent                   # 205
        HTTPPartialContent                 # 206
    HTTPRedirection                    # 3xx
        HTTPMultipleChoice                 # 300
        HTTPMovedPermanently               # 301
        HTTPFound                          # 302
        HTTPSeeOther                       # 303
        HTTPNotModified                    # 304
        HTTPUseProxy                       # 305
        HTTPTemporaryRedirect              # 307
    HTTPClientError                    # 4xx
        HTTPBadRequest                     # 400
        HTTPUnauthorized                   # 401
        HTTPPaymentRequired                # 402
        HTTPForbidden                      # 403
        HTTPNotFound                       # 404
        HTTPMethodNotAllowed               # 405
        HTTPNotAcceptable                  # 406
        HTTPProxyAuthenticationRequired    # 407
        HTTPRequestTimeOut                 # 408
        HTTPConflict                       # 409
        HTTPGone                           # 410
        HTTPLengthRequired                 # 411
        HTTPPreconditionFailed             # 412
        HTTPRequestEntityTooLarge          # 413
        HTTPRequestURITooLong              # 414
        HTTPUnsupportedMediaType           # 415
        HTTPRequestedRangeNotSatisfiable   # 416
        HTTPExpectationFailed              # 417
    HTTPServerError                    # 5xx
        HTTPInternalServerError            # 500
        HTTPNotImplemented                 # 501
        HTTPBadGateway                     # 502
        HTTPServiceUnavailable             # 503
        HTTPGatewayTimeOut                 # 504
        HTTPVersionNotSupported            # 505
需要 登录 后方可回复, 如果你还没有账号请 注册新账号