Skip to content

Commit e6657cc

Browse files
NHDalynickrobinson251Drvi
committed
Change logs to use current_exceptions_to_string() instead of exception= pattern (#1092)
* Change logs to use `current_exceptions_to_string()` instead of `exception=` * cleanup unused variable `catch e` * Update Exceptions.jl for older julia * Rename err => msg; fix missing import * Make "Don't retry on internal exceptions" less flakey --------- Co-authored-by: Nick Robinson <[email protected]> Co-authored-by: Tomáš Drvoštěp <[email protected]>
1 parent cefae7b commit e6657cc

File tree

9 files changed

+42
-29
lines changed

9 files changed

+42
-29
lines changed

src/Connections.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,9 +478,9 @@ function keepalive!(tcp)
478478
Base.iolock_begin()
479479
try
480480
Base.check_open(tcp)
481-
err = ccall(:uv_tcp_keepalive, Cint, (Ptr{Nothing}, Cint, Cuint),
481+
msg = ccall(:uv_tcp_keepalive, Cint, (Ptr{Nothing}, Cint, Cuint),
482482
tcp.handle, 1, 1)
483-
Base.uv_error("failed to set keepalive on tcp socket", err)
483+
Base.uv_error("failed to set keepalive on tcp socket", msg)
484484
finally
485485
Base.iolock_end()
486486
end

src/Exceptions.jl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,13 @@ function current_exceptions_to_string()
100100
buf = IOBuffer()
101101
println(buf)
102102
println(buf, "\n===========================\nHTTP Error message:\n")
103-
Base.display_error(buf, Base.catch_stack())
103+
exc = @static if VERSION >= v"1.8.0-"
104+
Base.current_exceptions()
105+
else
106+
Base.catch_stack()
107+
end
108+
Base.display_error(buf, exc)
104109
return String(take!(buf))
105110
end
106111

107-
end # module Exceptions
112+
end # module Exceptions

src/Servers.jl

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,9 @@ shutdown(::Nothing) = nothing
192192
function shutdown(fn::Function)
193193
try
194194
fn()
195-
catch e
196-
@error "shutdown function $fn failed" exception=(e, catch_backtrace())
195+
catch
196+
msg = current_exceptions_to_string()
197+
@error "shutdown function $fn failed. $msg"
197198
end
198199
end
199200

@@ -392,7 +393,8 @@ function listenloop(f, listener, conns, tcpisvalid,
392393
if e isa Base.IOError && e.code == Base.UV_ECONNABORTED
393394
verbose >= 0 && @infov 1 "Server on $(listener.hostname):$(listener.hostport) closing"
394395
else
395-
@errorv 2 "Server on $(listener.hostname):$(listener.hostport) errored" exception=(e, catch_backtrace())
396+
msg = current_exceptions_to_string()
397+
@errorv 2 "Server on $(listener.hostname):$(listener.hostport) errored. $msg"
396398
# quick little sleep in case there's a temporary
397399
# local error accepting and this might help avoid quickly re-erroring
398400
sleep(0.05 + rand() * 0.05)
@@ -431,7 +433,8 @@ function handle_connection(f, c::Connection, listener, readtimeout, access_log)
431433
if e isa ParseError
432434
write(c, Response(e.code == :HEADER_SIZE_EXCEEDS_LIMIT ? 431 : 400, string(e.code)))
433435
end
434-
@debugv 1 "handle_connection startread error" exception=(e, catch_backtrace())
436+
msg = current_exceptions_to_string()
437+
@debugv 1 "handle_connection startread error. $msg"
435438
break
436439
end
437440

@@ -458,7 +461,8 @@ function handle_connection(f, c::Connection, listener, readtimeout, access_log)
458461
# The remote can close the stream whenever it wants to, but there's nothing
459462
# anyone can do about it on this side. No reason to log an error in that case.
460463
level = e isa Base.IOError && !isopen(c) ? Logging.Debug : Logging.Error
461-
@logmsgv 1 level "handle_connection handler error" exception=(e, stacktrace(catch_backtrace()))
464+
msg = current_exceptions_to_string()
465+
@logmsgv 1 level "handle_connection handler error. $msg"
462466

463467
if isopen(http) && !iswritable(http)
464468
request.response.status = 500
@@ -473,9 +477,10 @@ function handle_connection(f, c::Connection, listener, readtimeout, access_log)
473477
end
474478
end
475479
end
476-
catch e
480+
catch
477481
# we should be catching everything inside the while loop, but just in case
478-
@errorv 1 "error while handling connection" exception=(e, catch_backtrace())
482+
msg = current_exceptions_to_string()
483+
@errorv 1 "error while handling connection. $msg"
479484
finally
480485
if readtimeout > 0
481486
wait_for_timeout[] = false

src/Streams.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ function IOExtras.readuntil(http::Stream, f::Function)::ByteView
326326
bytes = IOExtras.readuntil(http.stream, f)
327327
update_ntoread(http, length(bytes))
328328
return bytes
329-
catch e
329+
catch
330330
# if we error, it means we didn't find what we were looking for
331331
return UInt8[]
332332
end

src/WebSockets.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module WebSockets
33
using Base64, LoggingExtras, UUIDs, Sockets, Random
44
using MbedTLS: digest, MD_SHA1, SSLContext
55
using ..IOExtras, ..Streams, ..Connections, ..Messages, ..Conditions, ..Servers
6+
using ..Exceptions: current_exceptions_to_string
67
import ..open
78
import ..HTTP # for doc references
89

@@ -439,7 +440,8 @@ function upgrade(f::Function, http::Streams.Stream; suppress_close_error::Bool=f
439440
f(ws)
440441
catch e
441442
if !isok(e)
442-
suppress_close_error || @error "$(ws.id): Unexpected websocket server error" exception=(e, catch_backtrace())
443+
msg = current_exceptions_to_string()
444+
suppress_close_error || @error "$(ws.id): Unexpected websocket server error. $msg"
443445
end
444446
if !isclosed(ws)
445447
if e isa WebSocketError && e.message isa CloseFrameBody

src/clientlayers/ConnectionRequest.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ function connectionlayer(handler)
8080
io = newconnection(IOType, url.host, url.port; readtimeout=readtimeout, connect_timeout=connect_timeout, kw...)
8181
catch e
8282
if logerrors
83-
err = current_exceptions_to_string()
84-
@error err type=Symbol("HTTP.ConnectError") method=req.method url=req.url context=req.context logtag=logtag
83+
msg = current_exceptions_to_string()
84+
@error msg type=Symbol("HTTP.ConnectError") method=req.method url=req.url context=req.context logtag=logtag
8585
end
8686
req.context[:connect_errors] = get(req.context, :connect_errors, 0) + 1
8787
throw(ConnectError(string(url), e))
@@ -127,12 +127,12 @@ function connectionlayer(handler)
127127
root_err = ExceptionUnwrapping.unwrap_exception_to_root(e)
128128
# don't log if it's an HTTPError since we should have already logged it
129129
if logerrors && root_err isa StatusError
130-
err = current_exceptions_to_string()
131-
@error err type=Symbol("HTTP.StatusError") method=req.method url=req.url context=req.context logtag=logtag
130+
msg = current_exceptions_to_string()
131+
@error msg type=Symbol("HTTP.StatusError") method=req.method url=req.url context=req.context logtag=logtag
132132
end
133133
if logerrors && !ExceptionUnwrapping.has_wrapped_exception(e, HTTPError)
134-
err = current_exceptions_to_string(e)
135-
@error err type=Symbol("HTTP.ConnectionRequest") method=req.method url=req.url context=req.context logtag=logtag
134+
msg = current_exceptions_to_string()
135+
@error msg type=Symbol("HTTP.ConnectionRequest") method=req.method url=req.url context=req.context logtag=logtag
136136
end
137137
@debugv 1 "❗️ ConnectionLayer $root_err. Closing: $io"
138138
if @isdefined(stream) && stream.nwritten == -1

src/clientlayers/StreamRequest.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ function streamlayer(stream::Stream; iofunction=nothing, decompress::Union{Nothi
6868
end
6969
end
7070
end
71-
catch e
71+
catch
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()
76-
@error err type=Symbol("HTTP.IOError") method=req.method url=req.url context=req.context logtag=logtag
75+
msg = current_exceptions_to_string()
76+
@error msg type=Symbol("HTTP.IOError") method=req.method url=req.url context=req.context logtag=logtag
7777
end
7878
end
7979
rethrow()

src/clientlayers/TimeoutRequest.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module TimeoutRequest
22

33
using ..Connections, ..Streams, ..Exceptions, ..Messages
44
using LoggingExtras, ConcurrentUtilities
5+
using ..Exceptions: current_exceptions_to_string
56

67
export timeoutlayer
78

@@ -25,8 +26,8 @@ function timeoutlayer(handler)
2526
req = stream.message.request
2627
req.context[:timeout_errors] = get(req.context, :timeout_errors, 0) + 1
2728
if logerrors
28-
err = current_exceptions_to_string()
29-
@error err type=Symbol("HTTP.TimeoutError") method=req.method url=req.url context=req.context timeout=readtimeout logtag=logtag
29+
msg = current_exceptions_to_string()
30+
@error msg type=Symbol("HTTP.TimeoutError") method=req.method url=req.url context=req.context timeout=readtimeout logtag=logtag
3031
end
3132
e = Exceptions.TimeoutError(readtimeout)
3233
end

test/server.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,17 +191,17 @@ const echostreamhandler = HTTP.streamhandler(echohandler)
191191
HTTP.startwrite(http)
192192
write(http, "response body\n")
193193
end
194-
194+
195195
port = HTTP.port(server)
196-
196+
197197
sock = connect(host, port)
198198
close(sock)
199-
199+
200200
r = HTTP.get("https://$(host):$(port)/"; readtimeout=30, require_ssl_verification = false)
201201
@test r.status == 200
202202

203203
close(server)
204-
end
204+
end
205205
end # @testset
206206

207207
@testset "on_shutdown" begin
@@ -222,7 +222,7 @@ end # @testset
222222
# First shutdown function errors, second adds 1
223223
shutdown_throw() = throw(ErrorException("Broken"))
224224
server = HTTP.listen!(x -> nothing; listenany=true, on_shutdown=[shutdown_throw, shutdown_add])
225-
@test_logs (:error, r"shutdown function .* failed") close(server)
225+
@test_logs (:error, r"shutdown function .* failed.*ERROR: Broken.*"s) close(server)
226226
@test TEST_COUNT[] == 4
227227
end # @testset
228228

0 commit comments

Comments
 (0)