From b019786c5ae22947617d2f7b7e006cbcc7172eda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=20H=C3=A4ggblad?= Date: Thu, 5 Jan 2023 10:00:48 +0100 Subject: [PATCH] rust-sdk: replace a bunch of unwrap with error return --- sdk/rust/nym-sdk/src/error.rs | 7 ++++- sdk/rust/nym-sdk/src/mixnet/client.rs | 45 ++++++++++++++------------- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/sdk/rust/nym-sdk/src/error.rs b/sdk/rust/nym-sdk/src/error.rs index 5327aa142c..45c5c342a2 100644 --- a/sdk/rust/nym-sdk/src/error.rs +++ b/sdk/rust/nym-sdk/src/error.rs @@ -2,9 +2,14 @@ pub enum Error { #[error("i/o error: {0}")] IoError(#[from] std::io::Error), - + #[error("toml error: {0}")] + TomlError(#[from] toml::de::Error), + #[error(transparent)] + ClientCoreError(#[from] client_core::error::ClientCoreError), #[error("key file encountered that we don't want to overwrite")] DontOverwrite, + #[error("shared gateway key file encountered that we don't want to overwrite")] + DontOverwriteGatewayKey, } pub type Result = std::result::Result; diff --git a/sdk/rust/nym-sdk/src/mixnet/client.rs b/sdk/rust/nym-sdk/src/mixnet/client.rs index 92ad59b262..1444585dd7 100644 --- a/sdk/rust/nym-sdk/src/mixnet/client.rs +++ b/sdk/rust/nym-sdk/src/mixnet/client.rs @@ -111,7 +111,7 @@ impl Client { self.connection_state.gateway_endpoint_config() } - pub async fn register_with_gateway(&mut self) { + pub async fn register_with_gateway(&mut self) -> Result<()> { assert!( matches!(self.connection_state, ConnectionState::New), "can only setup gateway when in `New` connection state" @@ -122,8 +122,7 @@ impl Client { self.config.nym_api_endpoints.clone(), self.config.user_chosen_gateway.clone(), ) - .await - .expect("WIP"); + .await?; let nym_address = client_core::init::get_client_address(&self.key_manager, &gateway_config); @@ -131,21 +130,21 @@ impl Client { gateway_endpoint_config: gateway_config, nym_address, }; + Ok(()) } - fn write_gateway_key(&self, key_mode: &GatewayKeyMode) { + fn write_gateway_key(&self, key_mode: &GatewayKeyMode) -> Result<()> { let key_paths = self.key_paths.as_ref().unwrap().clone(); let path_finder = ClientKeyPathfinder::from(key_paths); if path_finder.gateway_key_file_exists() && key_mode.is_keep() { - todo!("WIP"); + return Err(Error::DontOverwriteGatewayKey); }; - self.key_manager - .store_key_gateway_only(&path_finder) - .expect("WIP"); + self.key_manager.store_key_gateway_only(&path_finder)?; + Ok(()) } - fn write_gateway_endpoint_config(&self) { + fn write_gateway_endpoint_config(&self) -> Result<()> { let key_paths = self.key_paths.as_ref().unwrap().clone(); let gateway_endpoint_config_path = key_paths.gateway_endpoint_config; let gateway_endpoint_config = @@ -153,17 +152,17 @@ impl Client { // Ensure the whole directory structure exists if let Some(parent_dir) = gateway_endpoint_config_path.parent() { - std::fs::create_dir_all(parent_dir).expect("WIP"); + std::fs::create_dir_all(parent_dir)?; } - std::fs::write(gateway_endpoint_config_path, gateway_endpoint_config).expect("WIP"); + std::fs::write(gateway_endpoint_config_path, gateway_endpoint_config)?; + Ok(()) } - fn read_gateway_endpoint_config(&mut self) { + fn read_gateway_endpoint_config(&mut self) -> Result<()> { let key_paths = self.key_paths.as_ref().unwrap().clone(); - let gateway_endpoint_config: GatewayEndpointConfig = toml::from_str( - &std::fs::read_to_string(key_paths.gateway_endpoint_config).expect("WIP"), - ) - .expect("WIP"); + let gateway_endpoint_config: GatewayEndpointConfig = + std::fs::read_to_string(key_paths.gateway_endpoint_config) + .map(|str| toml::from_str(&str))??; let nym_address = client_core::init::get_client_address(&self.key_manager, &gateway_endpoint_config); @@ -172,10 +171,11 @@ impl Client { gateway_endpoint_config, nym_address, }; + Ok(()) } /// Connects to the mixnet via the gateway in the client config - pub async fn connect_to_mixnet(&mut self) { + pub async fn connect_to_mixnet(&mut self) -> Result<()> { // For some simple cases we can figure how to setup gateway without it having to have been // called in advance. if matches!(self.connection_state, ConnectionState::New) { @@ -184,18 +184,18 @@ impl Client { // If we have a gateway key from client, then we can just read the corresponding // config println!("Has gateway key: loading"); - self.read_gateway_endpoint_config(); + self.read_gateway_endpoint_config()?; } else { // If we didn't find any shared gateway key during creation, that means we first // need to register a gateway println!("NO gateway key: registering new"); - self.register_with_gateway().await; - self.write_gateway_key(&GatewayKeyMode::Overwrite); - self.write_gateway_endpoint_config(); + self.register_with_gateway().await?; + self.write_gateway_key(&GatewayKeyMode::Overwrite)?; + self.write_gateway_endpoint_config()?; } } else { // If we don't have any key paths, just use ephemeral keys - self.register_with_gateway().await; + self.register_with_gateway().await?; } } @@ -243,6 +243,7 @@ impl Client { reconstructed_receiver, task_manager: started_client.task_manager, }; + Ok(()) } /// Sends stringy data to the supplied Nym address