Skip to content

Instantly share code, notes, and snippets.

@bparanj
Created December 10, 2017 20:14
Show Gist options
  • Save bparanj/3fe396bfb10a7eb779ee2accc7db6f89 to your computer and use it in GitHub Desktop.
Save bparanj/3fe396bfb10a7eb779ee2accc7db6f89 to your computer and use it in GitHub Desktop.
require 'net/http'
# Handling Network Connection Failure
begin
http = Net::HTTP.new('localhost', '3000')
http.open_timeout = 3
http.read_timeout = 3
http.get('/')
rescue Errno::ECONNREFUSED => e
puts 'The server is down.'
puts e.message
# Retry a few times and fail with connection refused error message
rescue Timeout::Error => e
puts 'Timeout error occurred.'
puts e.message
# Retry a few times and fail with timeout error message
end
@bparanj
Copy link
Author

bparanj commented Dec 11, 2017

Handing the network is down issue.

require 'net/http'

# Handling Network Connection Failure

begin
  http = Net::HTTP.new('www.google.com', '80')
  http.open_timeout = 3
  http.read_timeout = 3
  http.get('/')    
  puts 'Success'
rescue SocketError
  puts 'Network connectivity issue'
  # SocketError: Failed to open TCP connection to www.google.com:80 (getaddrinfo: nodename nor servname provided, or not known)
  # Network is not 100% reliable
rescue Errno::ECONNREFUSED => e
  puts 'The server is down.'
  puts e.message
  # Retry a few times and fail with connection refused error message
rescue Timeout::Error => e
  puts 'Timeout error occurred.'
  puts e.message
  # Retry a few times and fail with timeout error message
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment