SocketError
RubyERRORNotableNetwork
Socket or hostname resolution failure
Quick Answer
Verify the hostname is correct, DNS is reachable, and rescue SocketError alongside Errno network errors.
What this means
Raised for high-level socket errors, most commonly hostname resolution failures (getaddrinfo). SocketError is a subclass of StandardError and wraps errors from the socket library that are not mapped to Errno:: constants.
Why it happens
- 1Hostname cannot be resolved (typo, DNS failure, no network)
- 2getaddrinfo returns an error for the specified host/service
Fix
Rescue with context and validate hostnames
Rescue with context and validate hostnames
require 'resolv' def resolvable?(host) Resolv.getaddress(host) true rescue Resolv::ResolvError false end
Why this works
Resolv.getaddress pre-checks DNS resolution without opening a socket.
Code examples
Reproducing the errorruby
require 'socket'
TCPSocket.new('this.host.does.not.exist', 80)
# SocketError: getaddrinfo: Name or service not knownRescue SocketErrorruby
begin
Net::HTTP.get(URI('http://badhost.example/'))
rescue SocketError => e
puts "DNS failure: #{e.message}"
endFull network error rescueruby
rescue SocketError, Errno::ECONNREFUSED, Errno::ETIMEDOUT => e raise NetworkError, e.message
Same error in other languages
Sources
Official documentation ↗
Ruby Standard Library Documentation
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev