Skip to content
Merged
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
31 changes: 18 additions & 13 deletions crates/sdk/src/websocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,17 @@ 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 {
Comment thread
ResuBaka marked this conversation as resolved.
($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 @@ -241,12 +252,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 @@ -303,9 +308,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 @@ -314,13 +319,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 @@ -376,7 +381,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
Loading