From 8fb2eb6fd5f9d460b4fadd49ecaab75b8f5d5254 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Wed, 27 Apr 2016 13:25:33 +1000 Subject: [PATCH] Revert function uv_error (The EU_EAI_ codes apply only to DNS errors). Handle separatly the error codes returned by uv_getaddrinfo() and the status codes passed to uv_getaddrinfocb. Throw UVError when an unexpected code is received. --- base/libuv.jl | 19 +++++------------- base/socket.jl | 52 ++++++++++++++++++++++++++++++++------------------ 2 files changed, 38 insertions(+), 33 deletions(-) diff --git a/base/libuv.jl b/base/libuv.jl index 6764a2e8c2382..77eadd73ce60e 100644 --- a/base/libuv.jl +++ b/base/libuv.jl @@ -59,22 +59,13 @@ 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) - -function uv_error(prefix::AbstractString, c::Integer) - if c == UV_EAI_SYSTEM - throw(SystemError(prefix)) - elseif c == UV_EAI_MEMORY - throw(OutOfMemoryError()) - elseif c < 0 - throw(UVError(prefix,c)) - end - return nothing -end - +uv_error(prefix::AbstractString, c::Integer) = c < 0 ? throw(UVError(prefix,c)) : nothing show(io::IO, e::UVError) = print(io, e.prefix*": "*struverror(e)*" ("*uverrorname(e)*")") ## event loop ## diff --git a/base/socket.jl b/base/socket.jl index 3416a0910493c..31ad5481554ea 100644 --- a/base/socket.jl +++ b/base/socket.jl @@ -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}) @@ -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_getaddrinfodb received an unexpected status code", status)) else freeaddrinfo = addrinfo while addrinfo != C_NULL @@ -577,33 +587,37 @@ 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] + 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)) - -type DNSError <: Exception - uverror::UVError - host::AbstractString -end - -function show(io::IO, err::DNSError) - print(io, "DNSError: ", err.host, ", ") - show(io, err.uverror) -end - function getaddrinfo(host::ASCIIString) c = Condition() getaddrinfo(host) do IP notify(c,IP) end - ip = wait(c) - if isa(ip,UVError) - @assert ip.code in [UV_EAI_NONAME, UV_EAI_AGAIN, UV_EAI_FAIL, UV_EAI_NODATA] - throw(DNSError(ip, host)) + 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 ip::IPAddr + return r::IPAddr end getaddrinfo(host::AbstractString) = getaddrinfo(ascii(host))