From 5814c762dc9899df0df7e6e3e9c11ded50358a22 Mon Sep 17 00:00:00 2001 From: ResuBaka Date: Sun, 6 Jul 2025 12:35:03 +0200 Subject: [PATCH] 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; } },