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

Throw DNSError in getaddrinfo (hide UVError) #15879

Merged
merged 1 commit into from
May 6, 2016
Merged
Show file tree
Hide file tree
Changes from all 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: 4 additions & 2 deletions base/libuv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ type UVError <: Exception
UVError(p::AbstractString,code::Integer)=new(p,code)
end

struverror(err::UVError) = bytestring(ccall(:uv_strerror,Cstring,(Int32,),err.code))
uverrorname(err::UVError) = bytestring(ccall(:uv_err_name,Cstring,(Int32,),err.code))
struverror(err::Int32) = bytestring(ccall(:uv_strerror,Cstring,(Int32,),err))
struverror(err::UVError) = struverror(err.code)
uverrorname(err::Int32) = bytestring(ccall(:uv_err_name,Cstring,(Int32,),err))
uverrorname(err::UVError) = uverrorname(err.code)

uv_error(prefix::Symbol, c::Integer) = uv_error(string(prefix),c)
uv_error(prefix::AbstractString, c::Integer) = c < 0 ? throw(UVError(prefix,c)) : nothing
Expand Down
40 changes: 34 additions & 6 deletions base/socket.jl
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,16 @@ end

##

type DNSError <: Exception
host::AbstractString
code::Int32
end

function show(io::IO, err::DNSError)
print(io, "DNSError: ", err.host, ", ", struverror(err.code),
" (", uverrorname(err.code), ")")
end

callback_dict = ObjectIdDict()

function uv_getaddrinfocb(req::Ptr{Void}, status::Cint, addrinfo::Ptr{Void})
Expand All @@ -553,7 +563,7 @@ function uv_getaddrinfocb(req::Ptr{Void}, status::Cint, addrinfo::Ptr{Void})
cb = unsafe_pointer_to_objref(data)::Function
pop!(callback_dict,cb) # using pop forces an error if cb not in callback_dict
if status != 0 || addrinfo == C_NULL
cb(UVError("getaddrinfo callback",status))
cb(UVError("uv_getaddrinfocb received an unexpected status code", status))
else
freeaddrinfo = addrinfo
while addrinfo != C_NULL
Expand All @@ -577,8 +587,16 @@ end

function getaddrinfo(cb::Function, host::ASCIIString)
callback_dict[cb] = cb
uv_error("getaddrinfo",ccall(:jl_getaddrinfo, Int32, (Ptr{Void}, Cstring, Ptr{UInt8}, Any, Ptr{Void}),
eventloop(), host, C_NULL, cb, uv_jl_getaddrinfocb::Ptr{Void}))
status = ccall(:jl_getaddrinfo, Int32, (Ptr{Void}, Cstring, Ptr{UInt8}, Any, Ptr{Void}),
eventloop(), host, C_NULL, cb, uv_jl_getaddrinfocb::Ptr{Void})
if status == UV_EINVAL
throw(ArgumentError("Invalid uv_getaddrinfo() agument"))
elseif status in [UV_ENOMEM, UV_ENOBUFS]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ENOBUFS isn't really an OutOfMemoryError

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is in this case.

throw(OutOfMemoryError())
elseif status < 0
throw(UVError("uv_getaddrinfo returned an unexpected error code", status))
end
return nothing
end
getaddrinfo(cb::Function, host::AbstractString) = getaddrinfo(cb,ascii(host))

Expand All @@ -587,9 +605,19 @@ function getaddrinfo(host::ASCIIString)
getaddrinfo(host) do IP
notify(c,IP)
end
ip = wait(c)
isa(ip,UVError) && throw(ip)
return ip::IPAddr
r = wait(c)
if isa(r,UVError)
if r.code in [UV_EAI_NONAME, UV_EAI_AGAIN, UV_EAI_FAIL, UV_EAI_NODATA]
throw(DNSError(host,r.code))
elseif r.code == UV_EAI_SYSTEM
throw(SystemError("uv_getaddrinfocb"))
elseif r.code == UV_EAI_MEMORY
throw(OutOfMemoryError())
else
throw(r)
end
end
return r::IPAddr
end
getaddrinfo(host::AbstractString) = getaddrinfo(ascii(host))

Expand Down
4 changes: 2 additions & 2 deletions test/socket.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ mktempdir() do tmpdir
end
end

@test_throws Base.UVError getaddrinfo(".invalid")
@test_throws Base.DNSError getaddrinfo(".invalid")
@test_throws ArgumentError getaddrinfo("localhost\0") # issue #10994
@test_throws Base.UVError connect("localhost", 21452)

Expand Down Expand Up @@ -137,7 +137,7 @@ port2, server2 = listenany(port)
close(server)
close(server2)

@test_throws Base.UVError connect(".invalid",80)
@test_throws Base.DNSError connect(".invalid",80)

begin
a = UDPSocket()
Expand Down