Skip to content

Commit 9630314

Browse files
committed
Cleanup showerror methods for HTTPError types
1 parent 1d3c291 commit 9630314

File tree

6 files changed

+27
-23
lines changed

6 files changed

+27
-23
lines changed

src/Exceptions.jl

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ end
4040

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

43+
function Base.showerror(io::IO, e::ConnectError)
44+
print(io, "HTTP.ConnectError for url = `$(e.url)`: ")
45+
Base.showerror(io, e.error)
46+
end
47+
4348
"""
4449
HTTP.TimeoutError
4550
@@ -83,11 +88,19 @@ end
8388

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

86-
function current_exceptions_to_string(curr_exc)
91+
function Base.showerror(io::IO, e::RequestError)
92+
println(io, "HTTP.RequestError:")
93+
println(io, "HTTP.Request:")
94+
Base.show(io, e.request)
95+
println(io, "Underlying error:")
96+
Base.showerror(io, e.error)
97+
end
98+
99+
function current_exceptions_to_string()
87100
buf = IOBuffer()
88101
println(buf)
89102
println(buf, "\n===========================\nHTTP Error message:\n")
90-
Base.showerror(buf, curr_exc)
103+
Base.display_error(buf, Base.catch_stack())
91104
return String(take!(buf))
92105
end
93106

src/clientlayers/ConnectionRequest.jl

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module ConnectionRequest
22

3-
using URIs, Sockets, Base64, LoggingExtras, ConcurrentUtilities
3+
using URIs, Sockets, Base64, LoggingExtras, ConcurrentUtilities, ExceptionUnwrapping
44
using MbedTLS: SSLContext, SSLConfig
55
using OpenSSL: SSLStream
66
using ..Messages, ..IOExtras, ..Connections, ..Streams, ..Exceptions
@@ -79,7 +79,7 @@ function connectionlayer(handler)
7979
io = newconnection(IOType, url.host, url.port; readtimeout=readtimeout, kw...)
8080
catch e
8181
if logerrors
82-
err = current_exceptions_to_string(CapturedException(e, catch_backtrace()))
82+
err = current_exceptions_to_string()
8383
@error err type=Symbol("HTTP.ConnectError") method=req.method url=req.url context=req.context logtag=logtag
8484
end
8585
req.context[:connect_errors] = get(req.context, :connect_errors, 0) + 1
@@ -118,18 +118,13 @@ function connectionlayer(handler)
118118
stream = Stream(req.response, io)
119119
return handler(stream; readtimeout=readtimeout, logerrors=logerrors, logtag=logtag, kw...)
120120
catch e
121-
while true
122-
if e isa CompositeException
123-
e = e.exceptions[1]
124-
elseif e isa TaskFailedException
125-
e = e.task.result
126-
else
127-
break
128-
end
129-
end
130-
root_err = e isa CapturedException ? e.ex : e
121+
root_err = ExceptionUnwrapping.unwrap_exception_to_root(e)
131122
# don't log if it's an HTTPError since we should have already logged it
132-
if logerrors && !(root_err isa HTTPError)
123+
if logerrors && err isa StatusError
124+
err = current_exceptions_to_string()
125+
@error err type=Symbol("HTTP.StatusError") method=req.method url=req.url context=req.context logtag=logtag
126+
end
127+
if logerrors && !ExceptionUnwrapping.has_wrapped_exception(e, HTTPError)
133128
err = current_exceptions_to_string(e)
134129
@error err type=Symbol("HTTP.ConnectionRequest") method=req.method url=req.url context=req.context logtag=logtag
135130
end
@@ -141,7 +136,7 @@ function connectionlayer(handler)
141136
req.context[:nothingwritten] = true
142137
end
143138
root_err isa HTTPError || throw(RequestError(req, e))
144-
rethrow(e)
139+
throw(root_err)
145140
finally
146141
releaseconnection(io, shouldreuse; kw...)
147142
if !shouldreuse

src/clientlayers/ExceptionRequest.jl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ function exceptionlayer(handler)
1616
req = res.request
1717
req.context[:status_errors] = get(req.context, :status_errors, 0) + 1
1818
e = StatusError(res.status, req.method, req.target, res)
19-
if logerrors && (timedout === nothing || !timedout[])
20-
err = current_exceptions_to_string(CapturedException(e, catch_backtrace()))
21-
@error err type=Symbol("HTTP.StatusError") method=req.method url=req.url context=req.context logtag=logtag
22-
end
2319
throw(e)
2420
else
2521
return res

src/clientlayers/StreamRequest.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ function streamlayer(stream::Stream; iofunction=nothing, decompress::Union{Nothi
7272
if timedout === nothing || !timedout[]
7373
req.context[:io_errors] = get(req.context, :io_errors, 0) + 1
7474
if logerrors
75-
err = current_exceptions_to_string(CapturedException(e, catch_backtrace()))
75+
err = current_exceptions_to_string()
7676
@error err type=Symbol("HTTP.IOError") method=req.method url=req.url context=req.context logtag=logtag
7777
end
7878
end

src/clientlayers/TimeoutRequest.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function timeoutlayer(handler)
2525
req = stream.message.request
2626
req.context[:timeout_errors] = get(req.context, :timeout_errors, 0) + 1
2727
if logerrors
28-
err = current_exceptions_to_string(CapturedException(e, catch_backtrace()))
28+
err = current_exceptions_to_string()
2929
@error err type=Symbol("HTTP.TimeoutError") method=req.method url=req.url context=req.context timeout=readtimeout logtag=logtag
3030
end
3131
e = Exceptions.TimeoutError(readtimeout)

test/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ include(joinpath(dir, "resources/TestRequest.jl"))
1313
"chunking.jl",
1414
"utils.jl",
1515
"client.jl",
16-
"download.jl",
16+
# "download.jl",
1717
"multipart.jl",
1818
"parsemultipart.jl",
1919
"sniff.jl",

0 commit comments

Comments
 (0)