From 5814c762dc9899df0df7e6e3e9c11ded50358a22 Mon Sep 17 00:00:00 2001 From: ResuBaka Date: Sun, 6 Jul 2025 12:35:03 +0200 Subject: [PATCH 1/2] refactor(sdk): improve the log message source line reference --- crates/sdk/src/websocket.rs | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/crates/sdk/src/websocket.rs b/crates/sdk/src/websocket.rs index 59b5c5e34f5..ade996dc755 100644 --- a/crates/sdk/src/websocket.rs +++ b/crates/sdk/src/websocket.rs @@ -200,6 +200,14 @@ fn request_insert_auth_header(req: &mut http::Request<()>, token: Option<&str>) } } +macro_rules! maybe_log_error { + ($cause:expr, $res:expr) => { + if let Err(e) = $res { + log::warn!("{}: {:?}", $cause, e); + } + }; +} + impl WsConnection { pub(crate) async fn connect( host: Uri, @@ -264,12 +272,6 @@ impl WsConnection { WebSocketMessage::Binary(bsatn::to_vec(&msg).unwrap().into()) } - fn maybe_log_error(cause: &str, res: std::result::Result) { - if let Err(e) = res { - log::warn!("{}: {:?}", cause, e); - } - } - async fn message_loop( mut self, incoming_messages: mpsc::UnboundedSender>, @@ -328,9 +330,9 @@ impl WsConnection { }, Err(e) => { - Self::maybe_log_error::<(), _>( + maybe_log_error!( "Error reading message from read WebSocket stream", - Err(e), + Result::<(), _>::Err(e) ); break; }, @@ -339,13 +341,13 @@ impl WsConnection { idle = false; record_metrics(bytes.len()); match Self::parse_response(&bytes) { - Err(e) => Self::maybe_log_error::<(), _>( + Err(e) => maybe_log_error!( "Error decoding WebSocketMessage::Binary payload", - Err(e), + Result::<(), _>::Err(e) ), - Ok(msg) => Self::maybe_log_error( + Ok(msg) => maybe_log_error!( "Error sending decoded message to incoming_messages queue", - incoming_messages.unbounded_send(msg), + incoming_messages.unbounded_send(msg) ), } } @@ -401,7 +403,7 @@ impl WsConnection { } } None => { - Self::maybe_log_error("Error sending close frame", SinkExt::close(&mut self.sock).await); + maybe_log_error!("Error sending close frame", SinkExt::close(&mut self.sock).await); outgoing_messages = None; } }, From 1a168f037a75feaafa8e70a432d5814fc95e8478 Mon Sep 17 00:00:00 2001 From: ResuBaka Date: Wed, 6 Aug 2025 20:00:45 +0200 Subject: [PATCH 2/2] refactor: add comment to maybe_log_error macro Co-authored-by: Phoebe Goldman Signed-off-by: ResuBaka --- crates/sdk/src/websocket.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/crates/sdk/src/websocket.rs b/crates/sdk/src/websocket.rs index b9ae0b8f9a1..f670a79a8b6 100644 --- a/crates/sdk/src/websocket.rs +++ b/crates/sdk/src/websocket.rs @@ -200,6 +200,9 @@ fn request_insert_auth_header(req: &mut http::Request<()>, token: Option<&str>) } } +/// If `res` evaluates to `Err(e)`, log a warning in the form `"{}: {:?}", $cause, e`. +/// +/// Could be trivially written as a function, but macro-ifying it preserves the source location of the log. macro_rules! maybe_log_error { ($cause:expr, $res:expr) => { if let Err(e) = $res {