Skip to content

Commit

Permalink
Return error when network does not yet exist
Browse files Browse the repository at this point in the history
  • Loading branch information
micbakos-rdx committed Sep 27, 2024
1 parent 712295d commit d6945ea
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 19 deletions.
9 changes: 7 additions & 2 deletions apple/Sources/Sargon/SargonOS/TestOS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

}
}

Expand Down
3 changes: 3 additions & 0 deletions crates/sargon/src/core/error/common_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
22 changes: 12 additions & 10 deletions crates/sargon/src/profile/logic/account/query_accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Accounts> {
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::<AccountsForDisplay>()
) -> Result<AccountsForDisplay> {
self.accounts_on_current_network().map(|accounts| {
accounts
.iter()
.map(AccountForDisplay::from)
.collect::<AccountsForDisplay>()
})
}

/// Looks up the account by account address, returns Err if the account is
Expand Down Expand Up @@ -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()
);
}
Expand All @@ -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
);
}
Expand All @@ -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)
Expand Down
11 changes: 7 additions & 4 deletions crates/sargon/src/profile/logic/gateway/current_gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
)
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/sargon/src/system/sargon_os/profile_state_holder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ impl ProfileStateHolder {
}

pub fn current_network(&self) -> Result<ProfileNetwork> {
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<Accounts> {
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,
Expand All @@ -76,7 +76,7 @@ impl ProfileStateHolder {
pub fn accounts_for_display_on_current_network(
&self,
) -> Result<AccountsForDisplay> {
self.access_profile_with(|p| {
self.try_access_profile_with(|p| {
p.accounts_for_display_on_current_network()
})
}
Expand Down

0 comments on commit d6945ea

Please sign in to comment.