Skip to content

Commit

Permalink
Cleanup showerror methods for HTTPError types
Browse files Browse the repository at this point in the history
  • Loading branch information
quinnj committed Jun 21, 2023
1 parent 1d3c291 commit 83a0fa5
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 24 deletions.
17 changes: 15 additions & 2 deletions src/Exceptions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ end

ExceptionUnwrapping.unwrap_exception(e::ConnectError) = e.error

function Base.showerror(io::IO, e::ConnectError)
print(io, "HTTP.ConnectError for url = `$(e.url)`: ")
Base.showerror(io, e.error)
end

"""
HTTP.TimeoutError
Expand Down Expand Up @@ -83,11 +88,19 @@ end

ExceptionUnwrapping.unwrap_exception(e::RequestError) = e.error

function current_exceptions_to_string(curr_exc)
function Base.showerror(io::IO, e::RequestError)
println(io, "HTTP.RequestError:")
println(io, "HTTP.Request:")
Base.show(io, e.request)
println(io, "Underlying error:")
Base.showerror(io, e.error)
end

function current_exceptions_to_string()
buf = IOBuffer()
println(buf)
println(buf, "\n===========================\nHTTP Error message:\n")
Base.showerror(buf, curr_exc)
Base.display_error(buf, Base.catch_stack())
return String(take!(buf))
end

Expand Down
25 changes: 10 additions & 15 deletions src/clientlayers/ConnectionRequest.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module ConnectionRequest

using URIs, Sockets, Base64, LoggingExtras, ConcurrentUtilities
using URIs, Sockets, Base64, LoggingExtras, ConcurrentUtilities, ExceptionUnwrapping
using MbedTLS: SSLContext, SSLConfig
using OpenSSL: SSLStream
using ..Messages, ..IOExtras, ..Connections, ..Streams, ..Exceptions
Expand Down Expand Up @@ -79,7 +79,7 @@ function connectionlayer(handler)
io = newconnection(IOType, url.host, url.port; readtimeout=readtimeout, kw...)
catch e
if logerrors
err = current_exceptions_to_string(CapturedException(e, catch_backtrace()))
err = current_exceptions_to_string()
@error err type=Symbol("HTTP.ConnectError") method=req.method url=req.url context=req.context logtag=logtag
end
req.context[:connect_errors] = get(req.context, :connect_errors, 0) + 1
Expand Down Expand Up @@ -118,18 +118,13 @@ function connectionlayer(handler)
stream = Stream(req.response, io)
return handler(stream; readtimeout=readtimeout, logerrors=logerrors, logtag=logtag, kw...)
catch e
while true
if e isa CompositeException
e = e.exceptions[1]
elseif e isa TaskFailedException
e = e.task.result
else
break
end
end
root_err = e isa CapturedException ? e.ex : e
root_err = ExceptionUnwrapping.unwrap_exception_to_root(e)
# don't log if it's an HTTPError since we should have already logged it
if logerrors && !(root_err isa HTTPError)
if logerrors && err isa StatusError
err = current_exceptions_to_string()
@error err type=Symbol("HTTP.StatusError") method=req.method url=req.url context=req.context logtag=logtag
end
if logerrors && !ExceptionUnwrapping.has_wrapped_exception(e, HTTPError)
err = current_exceptions_to_string(e)
@error err type=Symbol("HTTP.ConnectionRequest") method=req.method url=req.url context=req.context logtag=logtag
end
Expand All @@ -140,8 +135,8 @@ function connectionlayer(handler)
# idempotency of the request
req.context[:nothingwritten] = true
end
root_err isa HTTPError || throw(RequestError(req, e))
rethrow(e)
root_err isa HTTPError || throw(RequestError(req, root_err))
throw(root_err)
finally
releaseconnection(io, shouldreuse; kw...)
if !shouldreuse
Expand Down
4 changes: 0 additions & 4 deletions src/clientlayers/ExceptionRequest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ function exceptionlayer(handler)
req = res.request
req.context[:status_errors] = get(req.context, :status_errors, 0) + 1
e = StatusError(res.status, req.method, req.target, res)
if logerrors && (timedout === nothing || !timedout[])
err = current_exceptions_to_string(CapturedException(e, catch_backtrace()))
@error err type=Symbol("HTTP.StatusError") method=req.method url=req.url context=req.context logtag=logtag
end
throw(e)
else
return res
Expand Down
2 changes: 1 addition & 1 deletion src/clientlayers/StreamRequest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function streamlayer(stream::Stream; iofunction=nothing, decompress::Union{Nothi
if timedout === nothing || !timedout[]
req.context[:io_errors] = get(req.context, :io_errors, 0) + 1
if logerrors
err = current_exceptions_to_string(CapturedException(e, catch_backtrace()))
err = current_exceptions_to_string()
@error err type=Symbol("HTTP.IOError") method=req.method url=req.url context=req.context logtag=logtag
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/clientlayers/TimeoutRequest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function timeoutlayer(handler)
req = stream.message.request
req.context[:timeout_errors] = get(req.context, :timeout_errors, 0) + 1
if logerrors
err = current_exceptions_to_string(CapturedException(e, catch_backtrace()))
err = current_exceptions_to_string()
@error err type=Symbol("HTTP.TimeoutError") method=req.method url=req.url context=req.context timeout=readtimeout logtag=logtag
end
e = Exceptions.TimeoutError(readtimeout)
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ include(joinpath(dir, "resources/TestRequest.jl"))
"chunking.jl",
"utils.jl",
"client.jl",
"download.jl",
# "download.jl",
"multipart.jl",
"parsemultipart.jl",
"sniff.jl",
Expand Down

0 comments on commit 83a0fa5

Please sign in to comment.