Skip to content

refactor(sdk): improve the log message source line reference #2924

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions crates/sdk/src/websocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -264,12 +272,6 @@ impl WsConnection {
WebSocketMessage::Binary(bsatn::to_vec(&msg).unwrap().into())
}

fn maybe_log_error<T, U: std::fmt::Debug>(cause: &str, res: std::result::Result<T, U>) {
if let Err(e) = res {
log::warn!("{cause}: {e:?}");
}
}

async fn message_loop(
mut self,
incoming_messages: mpsc::UnboundedSender<ServerMessage<BsatnFormat>>,
Expand Down Expand Up @@ -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;
},
Expand All @@ -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)
),
}
}
Expand Down Expand Up @@ -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;
}
},
Expand Down