From a6003af25ca868f494de5e25799bc04a3eb2ed5a Mon Sep 17 00:00:00 2001 From: Eric Swanson Date: Fri, 18 Aug 2023 11:16:06 -0700 Subject: [PATCH] refacror: RequireIdentityExistsError --- src/dfx-core/src/error/identity/export_identity.rs | 4 ++-- .../identity/instantiate_identity_from_name.rs | 4 ++-- src/dfx-core/src/error/identity/mod.rs | 13 +------------ .../src/error/identity/new_identity_manager.rs | 4 ++-- src/dfx-core/src/error/identity/remove_identity.rs | 4 ++-- src/dfx-core/src/error/identity/rename_identity.rs | 4 ++-- .../src/error/identity/require_identity_exists.rs | 11 +++++++++++ .../src/error/identity/use_identity_by_name.rs | 4 ++-- src/dfx-core/src/identity/identity_manager.rs | 14 ++++++++++---- src/dfx/src/lib/error/mod.rs | 1 - 10 files changed, 34 insertions(+), 29 deletions(-) create mode 100644 src/dfx-core/src/error/identity/require_identity_exists.rs diff --git a/src/dfx-core/src/error/identity/export_identity.rs b/src/dfx-core/src/error/identity/export_identity.rs index 92b493c6b6..5a8f5c27ae 100644 --- a/src/dfx-core/src/error/identity/export_identity.rs +++ b/src/dfx-core/src/error/identity/export_identity.rs @@ -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; @@ -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), diff --git a/src/dfx-core/src/error/identity/instantiate_identity_from_name.rs b/src/dfx-core/src/error/identity/instantiate_identity_from_name.rs index 139dcae094..68f9992780 100644 --- a/src/dfx-core/src/error/identity/instantiate_identity_from_name.rs +++ b/src/dfx-core/src/error/identity/instantiate_identity_from_name.rs @@ -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)] @@ -11,5 +11,5 @@ pub enum InstantiateIdentityFromNameError { LoadIdentityFailed(LoadIdentityError), #[error("Identity must exist: {0}")] - RequireIdentityExistsFailed(IdentityError), + RequireIdentityExistsFailed(RequireIdentityExistsError), } diff --git a/src/dfx-core/src/error/identity/mod.rs b/src/dfx-core/src/error/identity/mod.rs index 302029e996..ba4412624b 100644 --- a/src/dfx-core/src/error/identity/mod.rs +++ b/src/dfx-core/src/error/identity/mod.rs @@ -19,21 +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 std::path::PathBuf; -use thiserror::Error; - -#[derive(Error, Debug)] -pub enum IdentityError { - #[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), -} diff --git a/src/dfx-core/src/error/identity/new_identity_manager.rs b/src/dfx-core/src/error/identity/new_identity_manager.rs index 137862014d..d5016e06c9 100644 --- a/src/dfx-core/src/error/identity/new_identity_manager.rs +++ b/src/dfx-core/src/error/identity/new_identity_manager.rs @@ -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; @@ -16,5 +16,5 @@ pub enum NewIdentityManagerError { InitializeFailed(InitializeIdentityManagerError), #[error("The specified identity must exist: {0}")] - OverrideIdentityMustExist(IdentityError), + OverrideIdentityMustExist(RequireIdentityExistsError), } diff --git a/src/dfx-core/src/error/identity/remove_identity.rs b/src/dfx-core/src/error/identity/remove_identity.rs index c4b43d6e08..a7e93fe0e1 100644 --- a/src/dfx-core/src/error/identity/remove_identity.rs +++ b/src/dfx-core/src/error/identity/remove_identity.rs @@ -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; @@ -28,5 +28,5 @@ pub enum RemoveIdentityError { RemoveIdentityFromKeyringFailed(KeyringError), #[error("Identity must exist: {0}")] - RequireIdentityExistsFailed(IdentityError), + RequireIdentityExistsFailed(RequireIdentityExistsError), } diff --git a/src/dfx-core/src/error/identity/rename_identity.rs b/src/dfx-core/src/error/identity/rename_identity.rs index e349a3b04a..658f728ce6 100644 --- a/src/dfx-core/src/error/identity/rename_identity.rs +++ b/src/dfx-core/src/error/identity/rename_identity.rs @@ -2,10 +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::write_default_identity::WriteDefaultIdentityError; -use crate::error::identity::IdentityError; use crate::error::keyring::KeyringError; use thiserror::Error; @@ -21,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), diff --git a/src/dfx-core/src/error/identity/require_identity_exists.rs b/src/dfx-core/src/error/identity/require_identity_exists.rs new file mode 100644 index 0000000000..12edc3f6c8 --- /dev/null +++ b/src/dfx-core/src/error/identity/require_identity_exists.rs @@ -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), +} diff --git a/src/dfx-core/src/error/identity/use_identity_by_name.rs b/src/dfx-core/src/error/identity/use_identity_by_name.rs index 20b124afc4..25d0f871a3 100644 --- a/src/dfx-core/src/error/identity/use_identity_by_name.rs +++ b/src/dfx-core/src/error/identity/use_identity_by_name.rs @@ -1,11 +1,11 @@ +use crate::error::identity::require_identity_exists::RequireIdentityExistsError; use crate::error::identity::write_default_identity::WriteDefaultIdentityError; -use crate::error::identity::IdentityError; use thiserror::Error; #[derive(Error, Debug)] pub enum UseIdentityByNameError { #[error("Identity must exist: {0}")] - RequireIdentityExistsFailed(IdentityError), + RequireIdentityExistsFailed(RequireIdentityExistsError), #[error("Failed to write default identity: {0}")] WriteDefaultIdentityFailed(WriteDefaultIdentityError), diff --git a/src/dfx-core/src/identity/identity_manager.rs b/src/dfx-core/src/identity/identity_manager.rs index 067c86c109..29bd8a8209 100644 --- a/src/dfx-core/src/identity/identity_manager.rs +++ b/src/dfx-core/src/identity/identity_manager.rs @@ -45,13 +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::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::identity::IdentityError; use crate::error::structured_file::StructuredFileError; use crate::foundation::get_user_home; use crate::fs::composite::ensure_parent_dir_exists; @@ -623,14 +623,20 @@ impl IdentityManager { /// 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); @@ -638,7 +644,7 @@ impl IdentityManager { 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, )) diff --git a/src/dfx/src/lib/error/mod.rs b/src/dfx/src/lib/error/mod.rs index 7051e55c7f..576c74fa65 100644 --- a/src/dfx/src/lib/error/mod.rs +++ b/src/dfx/src/lib/error/mod.rs @@ -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;