Skip to content

Commit

Permalink
refactor: move remaining IdentityError types to method-specific errors (
Browse files Browse the repository at this point in the history
#3383)

Move the remaining enumerated types in IdentityError to method-specific error types, and remove IdentityError.

New types:
- CallSenderFromWalletError
- RequireIdentityExistsError
- UseIdentityByNameError
- WriteDefaultIdentityError
  • Loading branch information
ericswanson-dfinity authored Sep 25, 2023
1 parent 2b7f4f1 commit b24cc40
Show file tree
Hide file tree
Showing 14 changed files with 87 additions and 48 deletions.
8 changes: 8 additions & 0 deletions src/dfx-core/src/error/identity/call_sender_from_wallet.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use candid::types::principal::PrincipalError;
use thiserror::Error;

#[derive(Error, Debug)]
pub enum CallSenderFromWalletError {
#[error("Failed to read principal from id '{0}': {1}")]
ParsePrincipalFromIdFailed(String, PrincipalError),
}
6 changes: 3 additions & 3 deletions src/dfx-core/src/error/identity/create_new_identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use crate::error::identity::load_pem_from_file::LoadPemFromFileError;
use crate::error::identity::remove_identity::RemoveIdentityError;
use crate::error::identity::save_identity_configuration::SaveIdentityConfigurationError;
use crate::error::identity::save_pem::SavePemError;
use crate::error::identity::use_identity_by_name::UseIdentityByNameError;
use crate::error::identity::validate_pem_file::ValidatePemFileError;
use crate::error::identity::IdentityError;
use thiserror::Error;

#[derive(Error, Debug)]
Expand Down Expand Up @@ -55,10 +55,10 @@ pub enum CreateNewIdentityError {
SavePemFailed(SavePemError),

#[error("Failed to switch back over to the identity you're replacing: {0}")]
SwitchBackToIdentityFailed(IdentityError),
SwitchBackToIdentityFailed(UseIdentityByNameError),

#[error("Failed to temporarily switch over to anonymous identity: {0}")]
SwitchToAnonymousIdentityFailed(IdentityError),
SwitchToAnonymousIdentityFailed(UseIdentityByNameError),

#[error("Failed to validate pem file: {0}")]
ValidatePemFileFailed(ValidatePemFileError),
Expand Down
4 changes: 2 additions & 2 deletions src/dfx-core/src/error/identity/export_identity.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::error::identity::get_identity_config_or_default::GetIdentityConfigOrDefaultError;
use crate::error::identity::load_pem::LoadPemError;
use crate::error::identity::require_identity_exists::RequireIdentityExistsError;
use crate::error::identity::validate_pem_file::ValidatePemFileError;
use crate::error::identity::IdentityError;
use std::string::FromUtf8Error;
use thiserror::Error;

Expand All @@ -11,7 +11,7 @@ pub enum ExportIdentityError {
GetIdentityConfigFailed(GetIdentityConfigOrDefaultError),

#[error("The specified identity does not exist: {0}")]
IdentityDoesNotExist(IdentityError),
IdentityDoesNotExist(RequireIdentityExistsError),

#[error("Failed to load pem file: {0}")]
LoadPemFailed(LoadPemError),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::error::identity::load_identity::LoadIdentityError;
use crate::error::identity::IdentityError;
use crate::error::identity::require_identity_exists::RequireIdentityExistsError;
use thiserror::Error;

#[derive(Error, Debug)]
Expand All @@ -11,5 +11,5 @@ pub enum InstantiateIdentityFromNameError {
LoadIdentityFailed(LoadIdentityError),

#[error("Identity must exist: {0}")]
RequireIdentityExistsFailed(IdentityError),
RequireIdentityExistsFailed(RequireIdentityExistsError),
}
24 changes: 4 additions & 20 deletions src/dfx-core/src/error/identity/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod call_sender_from_wallet;
pub mod convert_mnemonic_to_key;
pub mod create_identity_config;
pub mod create_new_identity;
Expand All @@ -18,27 +19,10 @@ pub mod new_identity_manager;
pub mod remove_identity;
pub mod rename_identity;
pub mod rename_wallet_global_config_key;
pub mod require_identity_exists;
pub mod save_identity_configuration;
pub mod save_pem;
pub mod use_identity_by_name;
pub mod validate_pem_file;
pub mod write_default_identity;
pub mod write_pem_to_file;

use crate::error::structured_file::StructuredFileError;
use ic_agent::export::PrincipalError;
use std::path::PathBuf;
use thiserror::Error;

#[derive(Error, Debug)]
pub enum IdentityError {
#[error("Identity {0} does not exist at '{1}'.")]
IdentityDoesNotExist(String, PathBuf),

#[error("Failed to read principal from id '{0}': {1}")]
ParsePrincipalFromIdFailed(String, PrincipalError),

#[error("An Identity named {0} cannot be created as it is reserved for internal use.")]
ReservedIdentityName(String),

#[error("Failed to save identity manager configuration: {0}")]
SaveIdentityManagerConfigurationFailed(StructuredFileError),
}
4 changes: 2 additions & 2 deletions src/dfx-core/src/error/identity/new_identity_manager.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::error::config::ConfigError;
use crate::error::identity::initialize_identity_manager::InitializeIdentityManagerError;
use crate::error::identity::IdentityError;
use crate::error::identity::require_identity_exists::RequireIdentityExistsError;
use crate::error::structured_file::StructuredFileError;
use thiserror::Error;

Expand All @@ -16,5 +16,5 @@ pub enum NewIdentityManagerError {
InitializeFailed(InitializeIdentityManagerError),

#[error("The specified identity must exist: {0}")]
OverrideIdentityMustExist(IdentityError),
OverrideIdentityMustExist(RequireIdentityExistsError),
}
4 changes: 2 additions & 2 deletions src/dfx-core/src/error/identity/remove_identity.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::error::fs::FsError;
use crate::error::identity::IdentityError;
use crate::error::identity::require_identity_exists::RequireIdentityExistsError;
use crate::error::keyring::KeyringError;
use crate::error::wallet_config::WalletConfigError;
use thiserror::Error;
Expand Down Expand Up @@ -28,5 +28,5 @@ pub enum RemoveIdentityError {
RemoveIdentityFromKeyringFailed(KeyringError),

#[error("Identity must exist: {0}")]
RequireIdentityExistsFailed(IdentityError),
RequireIdentityExistsFailed(RequireIdentityExistsError),
}
7 changes: 4 additions & 3 deletions src/dfx-core/src/error/identity/rename_identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use crate::error::fs::FsError;
use crate::error::identity::get_identity_config_or_default::GetIdentityConfigOrDefaultError;
use crate::error::identity::load_pem::LoadPemError;
use crate::error::identity::map_wallets_to_renamed_identity::MapWalletsToRenamedIdentityError;
use crate::error::identity::require_identity_exists::RequireIdentityExistsError;
use crate::error::identity::save_identity_configuration::SaveIdentityConfigurationError;
use crate::error::identity::save_pem::SavePemError;
use crate::error::identity::IdentityError;
use crate::error::identity::write_default_identity::WriteDefaultIdentityError;
use crate::error::keyring::KeyringError;
use thiserror::Error;

Expand All @@ -20,7 +21,7 @@ pub enum RenameIdentityError {
IdentityAlreadyExists(),

#[error("Identity does not exist: {0}")]
IdentityDoesNotExist(IdentityError),
IdentityDoesNotExist(RequireIdentityExistsError),

#[error("Failed to load pem: {0}")]
LoadPemFailed(LoadPemError),
Expand All @@ -41,5 +42,5 @@ pub enum RenameIdentityError {
SavePemFailed(SavePemError),

#[error("Failed to switch over default identity settings: {0}")]
SwitchDefaultIdentitySettingsFailed(IdentityError),
SwitchDefaultIdentitySettingsFailed(WriteDefaultIdentityError),
}
11 changes: 11 additions & 0 deletions src/dfx-core/src/error/identity/require_identity_exists.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use std::path::PathBuf;
use thiserror::Error;

#[derive(Error, Debug)]
pub enum RequireIdentityExistsError {
#[error("Identity {0} does not exist at '{1}'.")]
IdentityDoesNotExist(String, PathBuf),

#[error("An Identity named {0} cannot be created as it is reserved for internal use.")]
ReservedIdentityName(String),
}
12 changes: 12 additions & 0 deletions src/dfx-core/src/error/identity/use_identity_by_name.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use crate::error::identity::require_identity_exists::RequireIdentityExistsError;
use crate::error::identity::write_default_identity::WriteDefaultIdentityError;
use thiserror::Error;

#[derive(Error, Debug)]
pub enum UseIdentityByNameError {
#[error("Identity must exist: {0}")]
RequireIdentityExistsFailed(RequireIdentityExistsError),

#[error("Failed to write default identity: {0}")]
WriteDefaultIdentityFailed(WriteDefaultIdentityError),
}
8 changes: 8 additions & 0 deletions src/dfx-core/src/error/identity/write_default_identity.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use crate::error::structured_file::StructuredFileError;
use thiserror::Error;

#[derive(Error, Debug)]
pub enum WriteDefaultIdentityError {
#[error("Failed to save identity manager configuration: {0}")]
SaveIdentityManagerConfigurationFailed(StructuredFileError),
}
34 changes: 25 additions & 9 deletions src/dfx-core/src/identity/identity_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,13 @@ use crate::error::identity::rename_identity::RenameIdentityError::{
GetIdentityConfigFailed, LoadPemFailed, MapWalletsToRenamedIdentityFailed,
RenameIdentityDirectoryFailed, SavePemFailed, SwitchDefaultIdentitySettingsFailed,
};
use crate::error::identity::require_identity_exists::RequireIdentityExistsError;
use crate::error::identity::save_identity_configuration::SaveIdentityConfigurationError;
use crate::error::identity::save_identity_configuration::SaveIdentityConfigurationError::EnsureIdentityConfigurationDirExistsFailed;
use crate::error::identity::IdentityError;
use crate::error::identity::use_identity_by_name::UseIdentityByNameError;
use crate::error::identity::use_identity_by_name::UseIdentityByNameError::WriteDefaultIdentityFailed;
use crate::error::identity::write_default_identity::WriteDefaultIdentityError;
use crate::error::identity::write_default_identity::WriteDefaultIdentityError::SaveIdentityManagerConfigurationFailed;
use crate::error::structured_file::StructuredFileError;
use crate::foundation::get_user_home;
use crate::fs::composite::ensure_parent_dir_exists;
Expand Down Expand Up @@ -595,40 +599,52 @@ impl IdentityManager {
}

/// Select an identity by name to use by default
pub fn use_identity_named(&mut self, log: &Logger, name: &str) -> Result<(), IdentityError> {
self.require_identity_exists(log, name)?;
self.write_default_identity(name)?;
pub fn use_identity_named(
&mut self,
log: &Logger,
name: &str,
) -> Result<(), UseIdentityByNameError> {
self.require_identity_exists(log, name)
.map_err(UseIdentityByNameError::RequireIdentityExistsFailed)?;
self.write_default_identity(name)
.map_err(WriteDefaultIdentityFailed)?;
self.configuration.default = name.to_string();
Ok(())
}

fn write_default_identity(&self, name: &str) -> Result<(), IdentityError> {
fn write_default_identity(&self, name: &str) -> Result<(), WriteDefaultIdentityError> {
let config = Configuration {
default: String::from(name),
};
save_configuration(&self.identity_json_path, &config)
.map_err(IdentityError::SaveIdentityManagerConfigurationFailed)?;
.map_err(SaveIdentityManagerConfigurationFailed)?;
Ok(())
}

/// Determines if there are enough files present to consider the identity as existing.
/// Does NOT guarantee that the identity will load correctly.
pub fn require_identity_exists(&self, log: &Logger, name: &str) -> Result<(), IdentityError> {
pub fn require_identity_exists(
&self,
log: &Logger,
name: &str,
) -> Result<(), RequireIdentityExistsError> {
trace!(log, "Checking if identity '{name}' exists.");
if name == ANONYMOUS_IDENTITY_NAME {
return Ok(());
}

if name.starts_with(TEMP_IDENTITY_PREFIX) {
return Err(IdentityError::ReservedIdentityName(String::from(name)));
return Err(RequireIdentityExistsError::ReservedIdentityName(
String::from(name),
));
}

let json_path = self.get_identity_json_path(name);
let plaintext_pem_path = self.file_locations.get_plaintext_identity_pem_path(name);
let encrypted_pem_path = self.file_locations.get_encrypted_identity_pem_path(name);

if !plaintext_pem_path.exists() && !encrypted_pem_path.exists() && !json_path.exists() {
Err(IdentityError::IdentityDoesNotExist(
Err(RequireIdentityExistsError::IdentityDoesNotExist(
String::from(name),
json_path,
))
Expand Down
8 changes: 4 additions & 4 deletions src/dfx-core/src/identity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
//! Wallets are a map of network-identity, but don't have their own types or manager
//! type.
use crate::config::directories::{get_config_dfx_dir_path, get_shared_network_data_directory};
use crate::error::identity::call_sender_from_wallet::CallSenderFromWalletError;
use crate::error::identity::call_sender_from_wallet::CallSenderFromWalletError::ParsePrincipalFromIdFailed;
use crate::error::identity::load_pem_identity::LoadPemIdentityError;
use crate::error::identity::load_pem_identity::LoadPemIdentityError::ReadIdentityFileFailed;
use crate::error::identity::map_wallets_to_renamed_identity::MapWalletsToRenamedIdentityError;
Expand All @@ -12,7 +14,6 @@ use crate::error::identity::new_hardware_identity::NewHardwareIdentityError::Ins
use crate::error::identity::new_identity::NewIdentityError;
use crate::error::identity::rename_wallet_global_config_key::RenameWalletGlobalConfigKeyError;
use crate::error::identity::rename_wallet_global_config_key::RenameWalletGlobalConfigKeyError::RenameWalletFailed;
use crate::error::identity::IdentityError;
use crate::error::wallet_config::WalletConfigError;
use crate::error::wallet_config::WalletConfigError::{
EnsureWalletConfigDirFailed, LoadWalletConfigFailed, SaveWalletConfigFailed,
Expand Down Expand Up @@ -304,11 +305,10 @@ pub enum CallSender {
// Determine whether the selected Identity
// or the provided wallet canister ID should be the Sender of the call.
impl CallSender {
pub fn from(wallet: &Option<String>) -> Result<Self, IdentityError> {
pub fn from(wallet: &Option<String>) -> Result<Self, CallSenderFromWalletError> {
let sender = if let Some(id) = wallet {
CallSender::Wallet(
Principal::from_text(id)
.map_err(|e| IdentityError::ParsePrincipalFromIdFailed(id.clone(), e))?,
Principal::from_text(id).map_err(|e| ParsePrincipalFromIdFailed(id.clone(), e))?,
)
} else {
CallSender::SelectedId
Expand Down
1 change: 0 additions & 1 deletion src/dfx/src/lib/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ pub mod project;

pub use build::BuildError;
pub use dfx_core::error::extension::ExtensionError;
pub use dfx_core::error::identity::IdentityError;
pub use notify_create_canister::NotifyCreateCanisterError;
pub use notify_top_up::NotifyTopUpError;
pub use project::ProjectError;
Expand Down

0 comments on commit b24cc40

Please sign in to comment.