From a0593e3aa9b19b9ca3c3881cbe0d9d207fe46d2c Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Tue, 26 Nov 2024 07:48:30 +0000 Subject: refactor: err msgs 'cannot' > 'failed to' in nearly all cases 'cannot' was used when an action was tried and failed. 'failed to' is strictly better because: * just because the action didn't work that time doesnt mean it cannot work * it is better at drawing the users attention to a problem --- src/lib/client.rs | 22 +++++------ src/lib/git/identify_ahead_behind.rs | 8 ++-- src/lib/git/mod.rs | 73 +++++++++++++++++++++--------------- src/lib/git/nostr_url.rs | 2 +- src/lib/git_events.rs | 4 +- src/lib/login/existing.rs | 8 ++-- src/lib/login/fresh.rs | 2 +- src/lib/login/mod.rs | 6 +-- src/lib/repo_ref.rs | 15 ++++---- 9 files changed, 77 insertions(+), 63 deletions(-) (limited to 'src/lib') diff --git a/src/lib/client.rs b/src/lib/client.rs index a8b48f4..7093dd5 100644 --- a/src/lib/client.rs +++ b/src/lib/client.rs @@ -176,7 +176,7 @@ impl Connect for Client { self.client .add_relay(relay_url) .await - .context("cannot add relay")?; + .context("failed to add relay")?; let relay = self.client.relay(relay_url).await?; @@ -263,7 +263,7 @@ impl Connect for Client { self.client .add_relay(relay.as_str()) .await - .context("cannot add relay")?; + .context("failed to add relay")?; } let relays_map = self.client.relays().await; @@ -374,7 +374,7 @@ impl Connect for Client { self.client .add_relay(relay.as_str()) .await - .context("cannot add relay")?; + .context("failed to add relay")?; } let dim = Style::new().color256(247); @@ -741,7 +741,7 @@ fn pb_after_style(succeed: bool) -> indicatif::ProgressStyle { async fn get_local_cache_database(git_repo_path: &Path) -> Result { NostrLMDB::open(git_repo_path.join(".git/nostr-cache.lmdb")) - .context("cannot open or create nostr cache database at .git/nostr-cache.lmdb") + .context("failed to open or create nostr cache database at .git/nostr-cache.lmdb") } async fn get_global_cache_database(git_repo_path: Option<&Path>) -> Result { @@ -753,13 +753,13 @@ async fn get_global_cache_database(git_repo_path: Option<&Path>) -> Result ) .await? .first() - .context("cannot find event in cache")? + .context("failed to find event in cache")? .clone()) } diff --git a/src/lib/git/identify_ahead_behind.rs b/src/lib/git/identify_ahead_behind.rs index c98c994..d736522 100644 --- a/src/lib/git/identify_ahead_behind.rs +++ b/src/lib/git/identify_ahead_behind.rs @@ -16,7 +16,7 @@ pub fn identify_ahead_behind( name.to_string(), git_repo .get_tip_of_branch(name) - .context(format!("cannot find from_branch '{name}'"))?, + .context(format!("failed to find from_branch '{name}'"))?, ), None => ( if let Ok(name) = git_repo.get_checked_out_branch_name() { @@ -38,7 +38,7 @@ pub fn identify_ahead_behind( name.to_string(), git_repo .get_tip_of_branch(name) - .context(format!("cannot find to_branch '{name}'"))?, + .context(format!("failed to find to_branch '{name}'"))?, ), None => { let (name, commit) = git_repo @@ -82,7 +82,7 @@ mod tests { identify_ahead_behind(&git_repo, &Some(branch_name.to_string()), &None) .unwrap_err() .to_string(), - format!("cannot find from_branch '{}'", &branch_name), + format!("failed to find from_branch '{}'", &branch_name), ); Ok(()) } @@ -98,7 +98,7 @@ mod tests { identify_ahead_behind(&git_repo, &None, &Some(branch_name.to_string())) .unwrap_err() .to_string(), - format!("cannot find to_branch '{}'", &branch_name), + format!("failed to find to_branch '{}'", &branch_name), ); Ok(()) } diff --git a/src/lib/git/mod.rs b/src/lib/git/mod.rs index 45ac58c..a49d306 100644 --- a/src/lib/git/mod.rs +++ b/src/lib/git/mod.rs @@ -99,16 +99,16 @@ impl RepoActions for Repo { self.git_repo .path() .parent() - .context("cannot find repositiory path as .git has no parent") + .context("failed to find repositiory path as .git has no parent") } fn get_origin_url(&self) -> Result { Ok(self .git_repo .find_remote("origin") - .context("cannot find origin")? + .context("failed to find origin")? .url() - .context("cannot find origin url")? + .context("failed to find origin url")? .to_string()) } @@ -116,7 +116,7 @@ impl RepoActions for Repo { let main_branch_name = { let remote_branches = self .get_remote_branch_names() - .context("cannot find any local branches")?; + .context("failed to find any local branches")?; if remote_branches.contains(&"origin/main".to_string()) { "origin/main" } else if remote_branches.contains(&"origin/master".to_string()) { @@ -129,7 +129,7 @@ impl RepoActions for Repo { let tip = self .get_tip_of_branch(main_branch_name) .context(format!( - "branch {main_branch_name} was listed as a remote branch but cannot get its tip commit id", + "branch {main_branch_name} was listed as a remote branch but failed to get its tip commit id", ))?; Ok((main_branch_name, tip)) @@ -139,7 +139,7 @@ impl RepoActions for Repo { let main_branch_name = { let local_branches = self .get_local_branch_names() - .context("cannot find any local branches")?; + .context("failed to find any local branches")?; if local_branches.contains(&"main".to_string()) { "main" } else if local_branches.contains(&"master".to_string()) { @@ -152,7 +152,7 @@ impl RepoActions for Repo { let tip = self .get_tip_of_branch(main_branch_name) .context(format!( - "branch {main_branch_name} was listed as a local branch but cannot get its tip commit id", + "branch {main_branch_name} was listed as a local branch but failed to get its tip commit id", ))?; Ok((main_branch_name, tip)) @@ -217,13 +217,15 @@ impl RepoActions for Repo { let branch = if let Ok(branch) = self .git_repo .find_branch(branch_name, git2::BranchType::Local) - .context(format!("cannot find local branch {branch_name}")) + .context(format!("failed to find local branch {branch_name}")) { branch } else { self.git_repo .find_branch(branch_name, git2::BranchType::Remote) - .context(format!("cannot find local or remote branch {branch_name}"))? + .context(format!( + "failed to find local or remote branch {branch_name}" + ))? }; Ok(oid_to_sha1(&branch.into_reference().peel_to_commit()?.id())) } @@ -385,7 +387,7 @@ impl RepoActions for Repo { .context("failed to extract signature - perhaps there is no signature?")?; Ok(std::str::from_utf8(&sign) - .context("commit signature cannot be converted to a utf8 string")? + .context("commit signature failed to be converted to a utf8 string")? .to_owned()) } @@ -525,7 +527,7 @@ impl RepoActions for Repo { last_patch } else { self.checkout(branch_name) - .context("no patches and so cannot create a proposal branch")?; + .context("no patches and so failed to create a proposal branch")?; return Ok(vec![]); }, "parent-commit", @@ -533,7 +535,7 @@ impl RepoActions for Repo { // check patches can be applied if !self.does_commit_exist(&parent_commit_id)? { - bail!("cannot find parent commit ({parent_commit_id}). run git pull and try again.") + bail!("failed to find parent commit ({parent_commit_id}). run git pull and try again.") } // checkout branch @@ -644,7 +646,7 @@ impl RepoActions for Repo { None, None, ) - .context("cannot amend commit to produce new oid")?; + .context("failed to amend commit to produce new oid")?; } if !applied_oid.to_string().eq(commit_id) { bail!( @@ -669,12 +671,12 @@ impl RepoActions for Repo { &oid_to_sha1( &revspec .from() - .context("cannot get starting commit from specified value")? + .context("failed to get starting commit from specified value")? .id(), ), &self .get_head_commit() - .context("cannot get head commit with gitlib2")?, + .context("failed to get head commit with gitlib2")?, ) .context("specified commit is not an ancestor of current head")?; Ok(ahead) @@ -684,13 +686,13 @@ impl RepoActions for Repo { &oid_to_sha1( &revspec .from() - .context("cannot get starting commit of range from specified value")? + .context("failed to get starting commit of range from specified value")? .id(), ), &oid_to_sha1( &revspec .to() - .context("cannot get end of range commit from specified value")? + .context("failed to get end of range commit from specified value")? .id(), ), ) @@ -720,11 +722,13 @@ impl RepoActions for Repo { match if just_global { self.git_repo .config() - .context("cannot open git config")? + .context("failed to open git config")? .open_global() - .context("cannot open global git config")? + .context("failed to open global git config")? } else { - self.git_repo.config().context("cannot open git config")? + self.git_repo + .config() + .context("failed to open git config")? } .get_entry(item) { @@ -742,7 +746,7 @@ impl RepoActions for Repo { } Ok(Some( item.value() - .context("cannot find git config item")? + .context("failed to find git config item")? .to_string(), )) } @@ -754,15 +758,17 @@ impl RepoActions for Repo { if global { self.git_repo .config() - .context("cannot open git config")? + .context("failed to open git config")? .open_global() - .context("cannot open global git config")? + .context("failed to open global git config")? } else { - self.git_repo.config().context("cannot open git config")? + self.git_repo + .config() + .context("failed to open git config")? } .set_str(item, value) .context(format!( - "cannot set {} git config item {}", + "failed to set {} git config item {}", if global { "global" } else { "local" }, item ))?; @@ -777,14 +783,16 @@ impl RepoActions for Repo { if global { self.git_repo .config() - .context("cannot open git config")? + .context("failed to open git config")? .open_global() - .context("cannot open global git config")? + .context("failed to open global git config")? } else { - self.git_repo.config().context("cannot open git config")? + self.git_repo + .config() + .context("failed to open git config")? } .remove(item) - .context("cannot remove existing git config item")?; + .context("failed to remove existing git config item")?; Ok(true) } } @@ -880,7 +888,7 @@ pub fn save_git_config_item(git_repo: &Option<&Repo>, item: &str, value: &str) - git2::Config::open_default()? .open_global()? .set_str(item, value) - .context(format!("cannot set global git config item {}", item)) + .context(format!("failed to set global git config item {}", item)) } } @@ -893,7 +901,10 @@ pub fn remove_git_config_item(git_repo: &Option<&Repo>, item: &str) -> Result { if let Some(signer_info) = signer_info { (signer_info.clone(), SignerInfoSource::CommandLineArguments) } else { - bail!("cannot get signer from cli signer arguments because none were specified") + bail!("failed to get signer from cli signer arguments because none were specified") } } Some(SignerInfoSource::GitLocal) => { @@ -169,7 +169,9 @@ async fn get_signer( password.clone() } else { if !prompt_for_ncryptsec_password { - bail!("cannot login without prompts a nsec is encrypted with a password"); + bail!( + "failed to login without prompts a nsec is encrypted with a password" + ); } Interactor::default() .password(PromptPasswordParms::default().with_prompt("password")) diff --git a/src/lib/login/fresh.rs b/src/lib/login/fresh.rs index 194f638..62622a8 100644 --- a/src/lib/login/fresh.rs +++ b/src/lib/login/fresh.rs @@ -627,7 +627,7 @@ fn silently_save_to_git_config( let git_repo = if global { &None } else if git_repo.is_none() { - bail!("cannot update local git config wihout git_repo object") + bail!("failed to update local git config wihout git_repo object") } else { git_repo }; diff --git a/src/lib/login/mod.rs b/src/lib/login/mod.rs index d2725e7..00dbb17 100644 --- a/src/lib/login/mod.rs +++ b/src/lib/login/mod.rs @@ -61,12 +61,12 @@ fn print_logged_in_as( source: &SignerInfoSource, ) -> Result<()> { if !offline_mode && user_ref.metadata.created_at.eq(&Timestamp::from(0)) { - eprintln!("cannot find profile..."); + eprintln!("failed to find profile..."); } else if !offline_mode && user_ref.metadata.name.eq(&user_ref.public_key.to_bech32()?) { - eprintln!("cannot extract account name from account metadata..."); + eprintln!("failed to extract account name from account metadata..."); } else if !offline_mode && user_ref.relays.created_at.eq(&Timestamp::from(0)) { eprintln!( - "cannot find your relay list. consider using another nostr client to create one to enhance your nostr experience." + "failed to find your relay list. consider using another nostr client to create one to enhance your nostr experience." ); } eprintln!( diff --git a/src/lib/repo_ref.rs b/src/lib/repo_ref.rs index 84de185..05234e2 100644 --- a/src/lib/repo_ref.rs +++ b/src/lib/repo_ref.rs @@ -95,7 +95,7 @@ impl TryFrom for RepoRef { for pk in maintainers { r.maintainers.push( nostr_sdk::prelude::PublicKey::from_str(&pk) - .context(format!("cannot convert entry from maintainers tag {pk} into a valid nostr public key. it should be in hex format")) + .context(format!("failed to convert entry from maintainers tag {pk} into a valid nostr public key. it should be in hex format")) .context("invalid repository event")?, ); } @@ -421,8 +421,9 @@ pub fn extract_pks(pk_strings: Vec) -> Result> { let mut pks: Vec = vec![]; for s in pk_strings { pks.push( - nostr_sdk::prelude::PublicKey::from_bech32(s.clone()) - .context(format!("cannot convert {s} into a valid nostr public key"))?, + nostr_sdk::prelude::PublicKey::from_bech32(s.clone()).context(format!( + "failed to convert {s} into a valid nostr public key" + ))?, ); } Ok(pks) @@ -441,15 +442,15 @@ pub fn save_repo_config_to_yaml( .write(true) .truncate(true) .open(path) - .context("cannot open maintainers.yaml file with write and truncate options")? + .context("failed to open maintainers.yaml file with write and truncate options")? } else { - std::fs::File::create(path).context("cannot create maintainers.yaml file")? + std::fs::File::create(path).context("failed to create maintainers.yaml file")? }; let mut maintainers_npubs = vec![]; for m in maintainers { maintainers_npubs.push( m.to_bech32() - .context("cannot convert public key into npub")?, + .context("failed to convert public key into npub")?, ); } serde_yaml::to_writer( @@ -460,7 +461,7 @@ pub fn save_repo_config_to_yaml( relays, }, ) - .context("cannot write maintainers to maintainers.yaml file serde_yaml") + .context("failed to write maintainers to maintainers.yaml file serde_yaml") } #[cfg(test)] -- cgit v1.2.3