Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error message for getaddrinfo failure #8498

Merged
merged 7 commits into from
Jan 7, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions spec/std/socket/addrinfo_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ describe Socket::Addrinfo do
typeof(addrinfo).should eq(Socket::Addrinfo)
end
end

it "raises helpful message on getaddrinfo failure" do
expect_raises(Socket::Addrinfo::Error, "Hostname lookup for badhostname failed: ") do
Socket::Addrinfo.resolve("badhostname", 80, type: Socket::Type::DGRAM)
end
end
end

describe ".udp" do
Expand Down
14 changes: 8 additions & 6 deletions src/socket/addrinfo.cr
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ class Socket
class Error < Socket::Error
getter error_code : Int32

def self.new(error_code)
new error_code, "getaddrinfo: #{String.new(LibC.gai_strerror(error_code))}"
def self.new(error_code, domain)
rdp marked this conversation as resolved.
Show resolved Hide resolved
new error_code, String.new(LibC.gai_strerror(error_code)), domain
end

def initialize(@error_code, message)
super(message)
def initialize(@error_code, message, domain)
super("Hostname lookup for #{domain} failed: #{message}")
end
end

Expand Down Expand Up @@ -121,9 +121,11 @@ class Socket
when 0
# success
when LibC::EAI_NONAME
raise Error.new(LibC::EAI_NONAME, "No address found for #{domain}:#{service} over #{protocol}")
raise Error.new(LibC::EAI_NONAME, "No address found", domain)
rdp marked this conversation as resolved.
Show resolved Hide resolved
when LibC::EAI_SOCKTYPE, LibC::EAI_SERVICE
rdp marked this conversation as resolved.
Show resolved Hide resolved
raise Error.new(ret, "service #{service} protocol #{protocol}", domain)
else
raise Error.new(ret)
raise Error.new(ret, domain)
end

begin
Expand Down