Skip to content

Commit

Permalink
refacror: RequireIdentityExistsError
Browse files Browse the repository at this point in the history
  • Loading branch information
ericswanson-dfinity committed Sep 23, 2023
1 parent 5c7e272 commit a6003af
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 29 deletions.
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),
}
13 changes: 1 addition & 12 deletions src/dfx-core/src/error/identity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
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),
}
4 changes: 2 additions & 2 deletions src/dfx-core/src/error/identity/rename_identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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),
Expand Down
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),
}
4 changes: 2 additions & 2 deletions src/dfx-core/src/error/identity/use_identity_by_name.rs
Original file line number Diff line number Diff line change
@@ -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),
Expand Down
14 changes: 10 additions & 4 deletions src/dfx-core/src/identity/identity_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -623,22 +623,28 @@ 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);
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
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 a6003af

Please sign in to comment.