Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: RemoveIdentityError #3379

Merged
merged 1 commit into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/dfx-core/src/error/identity/create_new_identity.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::error::fs::FsError;
use crate::error::identity::convert_mnemonic_to_key::ConvertMnemonicToKeyError;
use crate::error::identity::generate_key::GenerateKeyError;
use crate::error::identity::remove_identity::RemoveIdentityError;
use crate::error::identity::save_pem::SavePemError;
use crate::error::identity::IdentityError;
use thiserror::Error;
Expand Down Expand Up @@ -38,7 +39,7 @@ pub enum CreateNewIdentityError {
LoadPemFromFileFailed(IdentityError),

#[error("Failed to remove identity: {0}")]
RemoveIdentityFailed(IdentityError),
RemoveIdentityFailed(RemoveIdentityError),

#[error("Failed to rename temporary directory to permanent identity directory: {0}")]
RenameTemporaryIdentityDirectoryFailed(FsError),
Expand Down
22 changes: 1 addition & 21 deletions src/dfx-core/src/error/identity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub mod generate_key;
pub mod get_legacy_credentials_pem_path;
pub mod initialize_identity_manager;
pub mod new_identity_manager;
pub mod remove_identity;
pub mod rename_identity;
pub mod save_pem;
pub mod write_pem_to_file;
Expand All @@ -23,21 +24,9 @@ use thiserror::Error;

#[derive(Error, Debug)]
pub enum IdentityError {
#[error("Cannot delete the default identity.")]
CannotDeleteDefaultIdentity(),

#[error("Cannot delete the anonymous identity.")]
CannotDeleteAnonymousIdentity(),

#[error("Failed to decrypt PEM file: {0}")]
DecryptPemFileFailed(PathBuf, EncryptionError),

#[error("Failed to display linked wallets: {0}")]
DisplayLinkedWalletsFailed(WalletConfigError),

#[error("If you want to remove an identity with configured wallets, please use the --drop-wallets flag.")]
DropWalletsFlagRequiredToRemoveIdentityWithWallets(),

#[error("Failed to ensure identity configuration directory exists: {0}")]
EnsureIdentityConfigurationDirExistsFailed(FsError),

Expand Down Expand Up @@ -74,15 +63,6 @@ pub enum IdentityError {
#[error("Failed to read pem file: {0}")]
ReadPemFileFailed(FsError),

#[error("Failed to remove identity directory: {0}")]
RemoveIdentityDirectoryFailed(FsError),

#[error("Failed to remove identity from keyring: {0}")]
RemoveIdentityFromKeyringFailed(KeyringError),

#[error("Failed to remove identity file: {0}")]
RemoveIdentityFileFailed(FsError),

#[error("Failed to rename '{0}' to '{1}' in the global wallet config: {2}")]
RenameWalletFailed(Box<String>, Box<String>, WalletConfigError),

Expand Down
32 changes: 32 additions & 0 deletions src/dfx-core/src/error/identity/remove_identity.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use crate::error::fs::FsError;
use crate::error::identity::IdentityError;
use crate::error::keyring::KeyringError;
use crate::error::wallet_config::WalletConfigError;
use thiserror::Error;

#[derive(Error, Debug)]
pub enum RemoveIdentityError {
#[error("Cannot delete the anonymous identity.")]
CannotDeleteAnonymousIdentity(),

#[error("Cannot delete the default identity.")]
CannotDeleteDefaultIdentity(),

#[error("Failed to display linked wallets: {0}")]
DisplayLinkedWalletsFailed(WalletConfigError),

#[error("If you want to remove an identity with configured wallets, please use the --drop-wallets flag.")]
DropWalletsFlagRequiredToRemoveIdentityWithWallets(),

#[error("Failed to remove identity directory: {0}")]
RemoveIdentityDirectoryFailed(FsError),

#[error("Failed to remove identity file: {0}")]
RemoveIdentityFileFailed(FsError),

#[error("Failed to remove identity from keyring: {0}")]
RemoveIdentityFromKeyringFailed(KeyringError),

#[error("Identity must exist: {0}")]
RequireIdentityExistsFailed(IdentityError),
}
22 changes: 13 additions & 9 deletions src/dfx-core/src/identity/identity_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,20 @@ use crate::error::identity::initialize_identity_manager::InitializeIdentityManag
};
use crate::error::identity::new_identity_manager::NewIdentityManagerError;
use crate::error::identity::new_identity_manager::NewIdentityManagerError::LoadIdentityManagerConfigurationFailed;
use crate::error::identity::remove_identity::RemoveIdentityError;
use crate::error::identity::remove_identity::RemoveIdentityError::{
DisplayLinkedWalletsFailed, DropWalletsFlagRequiredToRemoveIdentityWithWallets,
RemoveIdentityDirectoryFailed, RemoveIdentityFileFailed,
};
use crate::error::identity::rename_identity::RenameIdentityError;
use crate::error::identity::rename_identity::RenameIdentityError::{
GetIdentityConfigFailed, LoadPemFailed, MapWalletsToRenamedIdentityFailed,
RenameIdentityDirectoryFailed, SavePemFailed, SwitchDefaultIdentitySettingsFailed,
};
use crate::error::identity::IdentityError;
use crate::error::identity::IdentityError::{
DisplayLinkedWalletsFailed, DropWalletsFlagRequiredToRemoveIdentityWithWallets,
EnsureIdentityConfigurationDirExistsFailed, GenerateFreshEncryptionConfigurationFailed,
GetIdentityPrincipalFailed, LoadIdentityConfigurationFailed, RemoveIdentityDirectoryFailed,
RemoveIdentityFileFailed, RemoveIdentityFromKeyringFailed, SaveIdentityConfigurationFailed,
GetIdentityPrincipalFailed, LoadIdentityConfigurationFailed, SaveIdentityConfigurationFailed,
};
use crate::error::structured_file::StructuredFileError;
use crate::foundation::get_user_home;
Expand Down Expand Up @@ -478,15 +481,16 @@ impl IdentityManager {
name: &str,
drop_wallets: bool,
display_linked_wallets_to: Option<&Logger>,
) -> Result<(), IdentityError> {
self.require_identity_exists(log, name)?;
) -> Result<(), RemoveIdentityError> {
self.require_identity_exists(log, name)
.map_err(RemoveIdentityError::RequireIdentityExistsFailed)?;

if name == ANONYMOUS_IDENTITY_NAME {
return Err(IdentityError::CannotDeleteAnonymousIdentity());
return Err(RemoveIdentityError::CannotDeleteAnonymousIdentity());
}

if self.configuration.default == name {
return Err(IdentityError::CannotDeleteDefaultIdentity());
return Err(RemoveIdentityError::CannotDeleteDefaultIdentity());
}

let wallet_config_file = self.get_persistent_wallet_config_file(name);
Expand All @@ -505,7 +509,7 @@ impl IdentityManager {
if let Ok(config) = self.get_identity_config_or_default(name) {
if let Some(suffix) = config.keyring_identity_suffix {
keyring_mock::delete_pem_from_keyring(&suffix)
.map_err(RemoveIdentityFromKeyringFailed)?;
.map_err(RemoveIdentityError::RemoveIdentityFromKeyringFailed)?;
}
}
remove_identity_file(&self.get_identity_json_path(name))?;
Expand Down Expand Up @@ -758,7 +762,7 @@ pub(super) fn save_identity_configuration(
}

/// Removes the file if it exists.
fn remove_identity_file(file: &Path) -> Result<(), IdentityError> {
fn remove_identity_file(file: &Path) -> Result<(), RemoveIdentityError> {
if file.exists() {
crate::fs::remove_file(file).map_err(RemoveIdentityFileFailed)?;
}
Expand Down