Skip to content

Commit

Permalink
Report connection error to on_event handler on native_tungstenite
Browse files Browse the repository at this point in the history
… backend (#26)

* native_tungstenite - Pass connection error back to on_event handler

* Add handling for ws_connect_blocking

* Move error handling to `ws_receive_impl` and `ws_connect_impl`

* Report errors or log them; not both

* Report errors or log them; not both

---------

Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
  • Loading branch information
rnd-ash and emilk committed Jan 24, 2024
1 parent dcb099a commit dfbbb0d
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions ewebsock/src/native_tungstenite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub(crate) fn ws_receive_impl(url: String, on_event: EventHandler) -> Result<()>
.name("ewebsock".to_owned())
.spawn(move || {
if let Err(err) = ws_receiver_blocking(&url, &on_event) {
log::error!("WebSocket error: {err}. Connection closed.");
on_event(WsEvent::Error(err));
} else {
log::debug!("WebSocket connection closed.");
}
Expand All @@ -71,7 +71,7 @@ pub fn ws_receiver_blocking(url: &str, on_event: &EventHandler) -> Result<()> {
let (mut socket, response) = match tungstenite::connect(url) {
Ok(result) => result,
Err(err) => {
return Err(err.to_string());
return Err(format!("Connect: {err}"));
}
};

Expand Down Expand Up @@ -107,7 +107,6 @@ pub fn ws_receiver_blocking(url: &str, on_event: &EventHandler) -> Result<()> {
},
Err(err) => {
let msg = format!("read: {err}");
on_event(WsEvent::Error(msg.clone()));
return Err(msg);
}
}
Expand All @@ -123,7 +122,7 @@ pub(crate) fn ws_connect_impl(url: String, on_event: EventHandler) -> Result<WsS
.name("ewebsock".to_owned())
.spawn(move || {
if let Err(err) = ws_connect_blocking(&url, &on_event, &rx) {
log::error!("WebSocket error: {err}. Connection closed.");
on_event(WsEvent::Error(err));
} else {
log::debug!("WebSocket connection closed.");
}
Expand All @@ -147,7 +146,7 @@ pub fn ws_connect_blocking(
let (mut socket, response) = match tungstenite::connect(url) {
Ok(result) => result,
Err(err) => {
return Err(err.to_string());
return Err(format!("Connect: {err}"));
}
};

Expand Down Expand Up @@ -232,7 +231,6 @@ pub fn ws_connect_blocking(
}
Err(err) => {
let msg = format!("read: {err}");
on_event(WsEvent::Error(msg.clone()));
return Err(msg);
}
}
Expand Down

0 comments on commit dfbbb0d

Please sign in to comment.