Exception: X::NetworkError

Inherits:
Error
  • Object
show all
Includes:
RequestContext
Defined in:
x-core/lib/x/core/errors/network_error.rb

Overview

Raised when a request never reached the API, or its response never arrived

It stands for every error a socket raises: a refused, reset, or dropped connection, a host that cannot be resolved, a TLS handshake that failed, a proxy that refused to open a tunnel, and the open, read, and write timeouts of the client. Whether the API acted on the request is unknown, so a client sends an idempotent request again after one, and leaves a POST to the caller; see Client#initialize.

The message names the request that failed, and #http_method and #uri read it, since nothing of a request that got no response is left to read it from.

Examples:

Tell a request that failed on the network from one the API refused

rescue X::NetworkError => e
  logger.warn("#{e.message}, giving up")

Instance Method Summary collapse

Constructor Details

#initialize(message = nil, http_method: nil, uri: nil) ⇒ NetworkError

Initialize a new NetworkError

Public, so that code that rescues a NetworkError can be tested with one built by hand, as Connection builds one for the errors a socket raises, or raised with no message, as any exception is. The error names the request, when given its method and URI, as x-core names the request that failed.

Examples:

Create a network error

error = X::NetworkError.new("Network error: Connection refused", http_method: :get, uri: URI("https://api.x.com/2/users/me"))

Parameters:

  • message (String, nil) (defaults to: nil) —

    what went wrong on the network, or nil for the name of the class, as an exception raised with no message is named, which names no request

  • http_method (Symbol, String, nil) (defaults to: nil) —

    the method of the request that failed, in any case

  • uri (URI::Generic, nil) (defaults to: nil) —

    the URI of the request that failed



52
53
54
55
# File 'x-core/lib/x/core/errors/network_error.rb', line 52

def initialize(message = nil, http_method: nil, uri: nil)
  name_request(http_method, uri)
  super(message && message_naming_request(message))
end

Instance Method Details

#http_method ⇒ Symbol?

The HTTP method the request that failed was sent with

Examples:

Tell a read that failed from a write

writes_failed += 1 unless error.http_method.eql?(:get)

Returns:

  • (Symbol, nil) —

    the method, as :get, :post, :put, or :delete, or nil for an error built without one



# File 'x-core/lib/x/core/errors/network_error.rb', line 25

#uri ⇒ URI::Generic?

The URI the request that failed was sent to

Examples:

Count the failures of each host

failures[error.uri.host] += 1

Returns:

  • (URI::Generic, nil) —

    the URI, or nil for an error built without one



# File 'x-core/lib/x/core/errors/network_error.rb', line 25