From bc59f94c1bc56f810dc51f0d9d04b3cdbb177e3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=20H=C3=A4ggblad?= Date: Mon, 6 May 2024 11:56:30 +0200 Subject: [PATCH] Pass errors up the call stack --- common/client-core/src/client/mix_traffic/mod.rs | 15 +++++++-------- .../src/client/mix_traffic/transceiver.rs | 6 ++++++ common/client-core/src/error.rs | 8 ++++++++ common/client-libs/gateway-client/src/error.rs | 3 +++ .../gateway-client/src/socket_state.rs | 6 +++--- 5 files changed, 27 insertions(+), 11 deletions(-) diff --git a/common/client-core/src/client/mix_traffic/mod.rs b/common/client-core/src/client/mix_traffic/mod.rs index 133794b225..6b52d40c01 100644 --- a/common/client-core/src/client/mix_traffic/mod.rs +++ b/common/client-core/src/client/mix_traffic/mod.rs @@ -85,18 +85,21 @@ impl MixTrafficController { error!("Failed to send sphinx packet(s) to the gateway: {err}"); self.consecutive_gateway_failure_count += 1; if self.consecutive_gateway_failure_count == MAX_FAILURE_COUNT { - // todo: in the future this should initiate a 'graceful' shutdown or try - // to reconnect? - panic!("failed to send sphinx packet to the gateway {MAX_FAILURE_COUNT} times in a row - assuming the gateway is dead. Can't do anything about it yet :(") + Err(ClientCoreError::GatewayMaxRetriesExceeded) + } else { + Err(ClientCoreError::GatewayClientSendError { + gateway_client_error: err.to_string(), + }) } } Ok(_) => { trace!("We *might* have managed to forward sphinx packet(s) to the gateway!"); self.consecutive_gateway_failure_count = 0; + Ok(()) } }; info!("JON: MixTrafficController: done sending sphinx packets to the gateway"); - Ok(r) + r } pub fn start_with_shutdown(mut self, mut shutdown: nym_task::TaskClient) { @@ -120,10 +123,6 @@ impl MixTrafficController { if let Err(err) = self.on_messages(mix_packets).await { log::error!("MixTrafficController: failed to send mix packets to the gateway: {err}"); } - // let r = tokio::time::timeout(tokio::time::Duration::from_secs(4), self.on_messages(mix_packets)).await; - // if r.is_err() { - // error!("MixTrafficController: Failed to send mix packets to the gateway"); - // } log::info!("JON: MixTrafficController: done with mix_rx recv"); }, None => { diff --git a/common/client-core/src/client/mix_traffic/transceiver.rs b/common/client-core/src/client/mix_traffic/transceiver.rs index b7202f031a..9bb970ed36 100644 --- a/common/client-core/src/client/mix_traffic/transceiver.rs +++ b/common/client-core/src/client/mix_traffic/transceiver.rs @@ -19,6 +19,12 @@ use futures::channel::{mpsc, oneshot}; #[error(transparent)] pub struct ErasedGatewayError(Box); +impl ErasedGatewayError { + pub fn downcast(&self) -> Option<&T> { + self.0.downcast_ref::() + } +} + fn erase_err(err: E) -> ErasedGatewayError { ErasedGatewayError(Box::new(err)) } diff --git a/common/client-core/src/error.rs b/common/client-core/src/error.rs index bdfecfda5e..195e5a8c35 100644 --- a/common/client-core/src/error.rs +++ b/common/client-core/src/error.rs @@ -78,6 +78,9 @@ pub enum ClientCoreError { source: tungstenite::Error, }, + #[error("max number of retries for gateway connection has been exceeded")] + GatewayMaxRetriesExceeded, + #[cfg(target_arch = "wasm32")] #[error("failed to establish gateway connection (wasm)")] GatewayJsConnectionFailure, @@ -88,6 +91,11 @@ pub enum ClientCoreError { #[error("timed out while trying to establish gateway connection")] GatewayConnectionTimeout, + #[error("failed to send sphinx packet to the gateway: {gateway_client_error}")] + GatewayClientSendError { + gateway_client_error: String, + }, + #[error("no ping measurements for the gateway ({identity}) performed")] NoGatewayMeasurements { identity: String }, diff --git a/common/client-libs/gateway-client/src/error.rs b/common/client-libs/gateway-client/src/error.rs index e2d9aae77c..28fd5900d0 100644 --- a/common/client-libs/gateway-client/src/error.rs +++ b/common/client-libs/gateway-client/src/error.rs @@ -74,6 +74,9 @@ pub enum GatewayClientError { #[error("Timed out")] Timeout, + #[error("timeout sending ws message to the gateway")] + TimeoutOnSendingWs, + #[error("Failed to send mixnet message")] MixnetMsgSenderFailedToSend, diff --git a/common/client-libs/gateway-client/src/socket_state.rs b/common/client-libs/gateway-client/src/socket_state.rs index 592aa392c6..29756eedf5 100644 --- a/common/client-libs/gateway-client/src/socket_state.rs +++ b/common/client-libs/gateway-client/src/socket_state.rs @@ -222,14 +222,14 @@ impl PartiallyDelegated { // Ok(r?) let r = tokio::time::timeout(Duration::from_secs(3), self.sink_half.send(msg)).await; let rr = match r { - Ok(rr) => rr, + Ok(rr) => Ok(rr?), Err(_) => { log::error!("JON: PartiallyDelegated::send_without_response - timeout sending a message"); - Ok(()) + Err(GatewayClientError::TimeoutOnSendingWs) } }; log::info!("JON: PartiallyDelegated::send_without_response - sent a message: {rr:?}"); - Ok(rr?) + rr } pub(crate) async fn batch_send_without_response(