Module: AeNetworkConnectionException

Defined in:
lib/ae_network_connection_exception.rb,
lib/ae_network_connection_exception/version.rb

Defined Under Namespace

Classes: ConnectionNotEstablished

Constant Summary collapse

VERSION =
"1.3.0"

Class Method Summary collapse

Class Method Details

.exception_signaturesObject

An array of examples for all the exceptions that we will catch



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ae_network_connection_exception.rb', line 33

def exception_signatures
  [
      SocketError.new('getaddrinfo: Name or service not known'),
      Errno::ECONNREFUSED.new('Connection refused - connect(2) for "example.com" port 443'),
      Errno::ETIMEDOUT.new('Connection timed out - connect(2) for "example.com" port 443'),
      Net::OpenTimeout.new('message'),
      Errno::ECONNRESET.new('Connection reset by peer - SSL_connect', Errno::ECONNREFUSED.new('Connection refused - connect(2) for "example.com" port 443')),
      Errno::EHOSTUNREACH.new('No route to host - connect(2) for "example.com" port 443'),
      Errno::ENETUNREACH.new('Network is unreachable - connect(2) for "example.com" port 443')
  ]
end

.tryObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ae_network_connection_exception.rb', line 10

def try
  yield
rescue SocketError, Net::OpenTimeout => e
  # SocketError happens when we fail to connect to a socket. Common problems here are DNS resolution (i.e. getaddrinfo)
  # Net::OpenTimeout happens when we are unable to establish an HTTP connection before the open_timeout

  raise ConnectionNotEstablished
rescue Errno::ETIMEDOUT, Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ENETUNREACH => e
  # Errno::ECONNREFUSED happens when we can not connect to the port
  # Errno::ETIMEDOUT happens when we timeout durring the tcp handshake
  # It is important to note, Errno::ETIMEDOUT can also happen after we have established a connection and are waiting for a response.
  # Because of this, we also check that the sys call that was made is connect(2).
  # Errno::* Exceptions have the following error message format
  # "#{Message string} - #{syscall} for "#{host}" port #{port}"

  raise_if_exception_message_matches(e, /connect\(2\)/)
rescue Errno::ECONNRESET => e
  # Errno::ECONNRESET happens when the connection is reset. This can happen during ssl negotiation
  
  raise_if_exception_message_matches(e, /SSL_connect/)
end