From 6512769d46a954b1c16cf6d06bf6e921e567098b Mon Sep 17 00:00:00 2001 From: micbakos-rdx Date: Tue, 17 Sep 2024 11:14:54 +0300 Subject: [PATCH] Return error when network does not yet exist --- apple/Sources/Sargon/SargonOS/TestOS.swift | 9 ++++++-- crates/sargon/src/core/error/common_error.rs | 3 +++ .../profile/logic/account/query_accounts.rs | 22 ++++++++++--------- .../profile/logic/gateway/current_gateway.rs | 11 ++++++---- .../system/sargon_os/profile_state_holder.rs | 6 ++--- 5 files changed, 32 insertions(+), 19 deletions(-) diff --git a/apple/Sources/Sargon/SargonOS/TestOS.swift b/apple/Sources/Sargon/SargonOS/TestOS.swift index 6dde616e5..f425da8ab 100644 --- a/apple/Sources/Sargon/SargonOS/TestOS.swift +++ b/apple/Sources/Sargon/SargonOS/TestOS.swift @@ -43,8 +43,13 @@ extension TestOS: SargonOSProtocol {} // MARK: Private extension TestOS { private func nextAccountName() throws -> DisplayName { - let index = try accountsForDisplayOnCurrentNetwork.count - return DisplayName(value: "Unnamed \(index)") + do { + let index = try accountsForDisplayOnCurrentNetwork.count + return DisplayName(value: "Unnamed \(index)") + } catch CommonError.NoNetworkInProfile(_) { + return DisplayName(value: "Unnamed 0") + } + } } diff --git a/crates/sargon/src/core/error/common_error.rs b/crates/sargon/src/core/error/common_error.rs index 0eb2fe126..0d6034d30 100644 --- a/crates/sargon/src/core/error/common_error.rs +++ b/crates/sargon/src/core/error/common_error.rs @@ -653,6 +653,9 @@ pub enum CommonError { "The provided entities do not derive from the given factor source" )] EntitiesNotDerivedByFactorSource = 10182, + + #[error("The network {network_id} does not exist in profile")] + NoNetworkInProfile { network_id: NetworkID } = 10183, } #[uniffi::export] diff --git a/crates/sargon/src/profile/logic/account/query_accounts.rs b/crates/sargon/src/profile/logic/account/query_accounts.rs index 56640e284..9d210107e 100644 --- a/crates/sargon/src/profile/logic/account/query_accounts.rs +++ b/crates/sargon/src/profile/logic/account/query_accounts.rs @@ -3,18 +3,20 @@ use crate::prelude::*; impl Profile { /// Returns the non-hidden accounts on the current network, empty if no accounts /// on the network - pub fn accounts_on_current_network(&self) -> Accounts { - self.current_network().accounts.non_hidden() + pub fn accounts_on_current_network(&self) -> Result { + self.current_network().map(|n| n.accounts.non_hidden()) } /// Returns the non-hidden accounts on the current network as `AccountForDisplay` pub fn accounts_for_display_on_current_network( &self, - ) -> AccountsForDisplay { - self.accounts_on_current_network() - .iter() - .map(AccountForDisplay::from) - .collect::() + ) -> Result { + self.accounts_on_current_network().map(|accounts| { + accounts + .iter() + .map(AccountForDisplay::from) + .collect::() + }) } /// Looks up the account by account address, returns Err if the account is @@ -44,7 +46,7 @@ mod tests { fn test_accounts_on_current_network() { let sut = SUT::sample(); assert_eq!( - sut.accounts_on_current_network(), + sut.accounts_on_current_network().unwrap(), Accounts::sample_mainnet() ); } @@ -53,7 +55,7 @@ mod tests { fn test_accounts_on_current_network_stokenet() { let sut = SUT::sample_other(); assert_eq!( - sut.accounts_on_current_network(), + sut.accounts_on_current_network().unwrap(), Accounts::just(Account::sample_stokenet_nadia()) // olivia is hidden ); } @@ -62,7 +64,7 @@ mod tests { fn test_accounts_for_display_on_current_network() { let sut = SUT::sample(); assert_eq!( - sut.accounts_for_display_on_current_network(), + sut.accounts_for_display_on_current_network().unwrap(), Accounts::sample_mainnet() .iter() .map(AccountForDisplay::from) diff --git a/crates/sargon/src/profile/logic/gateway/current_gateway.rs b/crates/sargon/src/profile/logic/gateway/current_gateway.rs index c8aa36272..a08189df3 100644 --- a/crates/sargon/src/profile/logic/gateway/current_gateway.rs +++ b/crates/sargon/src/profile/logic/gateway/current_gateway.rs @@ -16,10 +16,13 @@ impl Profile { /// The ProfileNetwork of the currently used Network dependent on the `current` /// Gateway set in AppPreferences. This affects which Accounts users see in /// "Home screen" in wallet apps. - pub fn current_network(&self) -> &ProfileNetwork { - self.networks - .get_id(self.current_network_id()) - .expect("Should have current network") + pub fn current_network(&self) -> Result<&ProfileNetwork> { + let current_network_id = self.current_network_id(); + self.networks.get_id(current_network_id).ok_or( + CommonError::NoNetworkInProfile { + network_id: current_network_id, + }, + ) } } diff --git a/crates/sargon/src/system/sargon_os/profile_state_holder.rs b/crates/sargon/src/system/sargon_os/profile_state_holder.rs index ebcb403e3..a93a8acf1 100644 --- a/crates/sargon/src/system/sargon_os/profile_state_holder.rs +++ b/crates/sargon/src/system/sargon_os/profile_state_holder.rs @@ -53,13 +53,13 @@ impl ProfileStateHolder { } pub fn current_network(&self) -> Result { - self.access_profile_with(|p| p.current_network().clone()) + self.try_access_profile_with(|p| p.current_network().map(|n| n.clone())) } /// Returns the non-hidden accounts on the current network, empty if no accounts /// on the network pub fn accounts_on_current_network(&self) -> Result { - self.access_profile_with(|p| p.accounts_on_current_network()) + self.try_access_profile_with(|p| p.accounts_on_current_network()) } /// Returns all the SecurityStructuresOfFactorSources, @@ -76,7 +76,7 @@ impl ProfileStateHolder { pub fn accounts_for_display_on_current_network( &self, ) -> Result { - self.access_profile_with(|p| { + self.try_access_profile_with(|p| { p.accounts_for_display_on_current_network() }) }