From 4b5da63ed38adf230a688c8328e9747883c9aa43 Mon Sep 17 00:00:00 2001 From: Andre Guedes Date: Mon, 26 Aug 2024 11:32:31 -0300 Subject: [PATCH] Expose function to extract safe message --- src/RustyObjectStore.jl | 69 ++++++++++++++++++++++++++++++++++------- 1 file changed, 57 insertions(+), 12 deletions(-) diff --git a/src/RustyObjectStore.jl b/src/RustyObjectStore.jl index 8f656c7..1b917b5 100644 --- a/src/RustyObjectStore.jl +++ b/src/RustyObjectStore.jl @@ -569,6 +569,13 @@ struct TimeoutError <: ErrorReason end struct ParseURLError <: ErrorReason end struct UnknownError <: ErrorReason end +reason_description(::ConnectionError) = "Connection" +reason_description(r::StatusError) = "StatusCode($(r.code))" +reason_description(::EarlyEOF) = "EarlyEOF" +reason_description(::TimeoutError) = "Timeout" +reason_description(::ParseURLError) = "ParseURL" +reason_description(::UnknownError) = "Unknown" + abstract type RequestException <: Exception end struct GetException <: RequestException msg::String @@ -590,21 +597,20 @@ struct DeleteException <: RequestException end -function reason(e::GetException) - return e.reason::ErrorReason -end +message(e::GetException) = e.msg::String +message(e::PutException) = e.msg::String +message(e::DeleteException) = e.msg::String -function reason(e::PutException) - return e.reason::ErrorReason +function message(e::Exception) + iobuf = IOBuffer() + Base.showerror(iobuf, e) + return String(take!(iobuf)) end -function reason(e::DeleteException) - return e.reason::ErrorReason -end - -function reason(e::Exception) - return UnknownError() -end +reason(e::GetException) = e.reason::ErrorReason +reason(e::PutException) = e.reason::ErrorReason +reason(e::DeleteException) = e.reason::ErrorReason +reason(e::Exception) = UnknownError() function status_code(e::Exception) return reason(e) isa StatusError ? reason(e).code : nothing @@ -630,6 +636,28 @@ function is_unknown(e::Exception) return reason(e) isa UnknownError end +function safe_message(e::Exception) + if e isa RequestException + msg = message(e) + r = reason(e) + if contains(msg, "") || contains(msg, "http") + # Contains unreadacted payload from backend or urls, try extracting safe information + code, backend_msg, report = extract_safe_parts(message(e)) + reason_str = reason_description(r) + + code = isnothing(code) ? "Unknown" : code + backend_msg = isnothing(backend_msg) ? "Error without safe message" : backend_msg + retry_report = isnothing(report) ? "" : "\n\n$(report)" + + return "$(backend_msg) (code: $(code), reason: $(reason_str))$(retry_report)" + else + # Assuming it safe as it does not come from backend or has url, return message directly + return msg + end + else + return nothing + end +end function rust_message_to_reason(msg::AbstractString) if ( @@ -682,6 +710,23 @@ function rust_message_to_reason(msg::AbstractString) end end +function extract_safe_parts(msg::AbstractString) + code = nothing + backend_message = nothing + retry_report = nothing + codemsg = match(r"[\s\S]*?([\s\S]*?)[\s\S]*?([\s\S]*?)(?:|\n)", msg) + if !isnothing(codemsg) + code = codemsg.captures[1] + backend_message = codemsg.captures[2] + end + retry_match = match(r"Recent attempts \([\s\S]*", msg) + if !isnothing(retry_match) + retry_report = retry_match.match + end + + return (code, backend_message, retry_report) +end + """ get_object!(buffer, path, conf) -> Int