Use anyhow
more idiomatically (#31052)
https://github.com/zed-industries/zed/issues/30972 brought up another case where our context is not enough to track the actual source of the issue: we get a general top-level error without inner error. The reason for this was `.ok_or_else(|| anyhow!("failed to read HEAD SHA"))?; ` on the top level. The PR finally reworks the way we use anyhow to reduce such issues (or at least make it simpler to bubble them up later in a fix). On top of that, uses a few more anyhow methods for better readability. * `.ok_or_else(|| anyhow!("..."))`, `map_err` and other similar error conversion/option reporting cases are replaced with `context` and `with_context` calls * in addition to that, various `anyhow!("failed to do ...")` are stripped with `.context("Doing ...")` messages instead to remove the parasitic `failed to` text * `anyhow::ensure!` is used instead of `if ... { return Err(...); }` calls * `anyhow::bail!` is used instead of `return Err(anyhow!(...));` Release Notes: - N/A
This commit is contained in:
parent
1e51a7ac44
commit
16366cf9f2
294 changed files with 2037 additions and 2610 deletions
|
@ -638,7 +638,7 @@ impl Platform for MacPlatform {
|
|||
Ok(())
|
||||
} else {
|
||||
let msg: id = msg_send![error, localizedDescription];
|
||||
Err(anyhow!("Failed to register: {:?}", msg))
|
||||
Err(anyhow!("Failed to register: {msg:?}"))
|
||||
};
|
||||
|
||||
if let Some(done_tx) = done_tx.take() {
|
||||
|
@ -832,11 +832,8 @@ impl Platform for MacPlatform {
|
|||
fn app_path(&self) -> Result<PathBuf> {
|
||||
unsafe {
|
||||
let bundle: id = NSBundle::mainBundle();
|
||||
if bundle.is_null() {
|
||||
Err(anyhow!("app is not running inside a bundle"))
|
||||
} else {
|
||||
Ok(path_from_objc(msg_send![bundle, bundlePath]))
|
||||
}
|
||||
anyhow::ensure!(!bundle.is_null(), "app is not running inside a bundle");
|
||||
Ok(path_from_objc(msg_send![bundle, bundlePath]))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -877,17 +874,11 @@ impl Platform for MacPlatform {
|
|||
fn path_for_auxiliary_executable(&self, name: &str) -> Result<PathBuf> {
|
||||
unsafe {
|
||||
let bundle: id = NSBundle::mainBundle();
|
||||
if bundle.is_null() {
|
||||
Err(anyhow!("app is not running inside a bundle"))
|
||||
} else {
|
||||
let name = ns_string(name);
|
||||
let url: id = msg_send![bundle, URLForAuxiliaryExecutable: name];
|
||||
if url.is_null() {
|
||||
Err(anyhow!("resource not found"))
|
||||
} else {
|
||||
ns_url_to_path(url)
|
||||
}
|
||||
}
|
||||
anyhow::ensure!(!bundle.is_null(), "app is not running inside a bundle");
|
||||
let name = ns_string(name);
|
||||
let url: id = msg_send![bundle, URLForAuxiliaryExecutable: name];
|
||||
anyhow::ensure!(!url.is_null(), "resource not found");
|
||||
ns_url_to_path(url)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1101,10 +1092,7 @@ impl Platform for MacPlatform {
|
|||
verb = "creating";
|
||||
status = SecItemAdd(attrs.as_concrete_TypeRef(), ptr::null_mut());
|
||||
}
|
||||
|
||||
if status != errSecSuccess {
|
||||
return Err(anyhow!("{} password failed: {}", verb, status));
|
||||
}
|
||||
anyhow::ensure!(status == errSecSuccess, "{verb} password failed: {status}");
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
|
@ -1131,24 +1119,24 @@ impl Platform for MacPlatform {
|
|||
match status {
|
||||
security::errSecSuccess => {}
|
||||
security::errSecItemNotFound | security::errSecUserCanceled => return Ok(None),
|
||||
_ => return Err(anyhow!("reading password failed: {}", status)),
|
||||
_ => anyhow::bail!("reading password failed: {status}"),
|
||||
}
|
||||
|
||||
let result = CFType::wrap_under_create_rule(result)
|
||||
.downcast::<CFDictionary>()
|
||||
.ok_or_else(|| anyhow!("keychain item was not a dictionary"))?;
|
||||
.context("keychain item was not a dictionary")?;
|
||||
let username = result
|
||||
.find(kSecAttrAccount as *const _)
|
||||
.ok_or_else(|| anyhow!("account was missing from keychain item"))?;
|
||||
.context("account was missing from keychain item")?;
|
||||
let username = CFType::wrap_under_get_rule(*username)
|
||||
.downcast::<CFString>()
|
||||
.ok_or_else(|| anyhow!("account was not a string"))?;
|
||||
.context("account was not a string")?;
|
||||
let password = result
|
||||
.find(kSecValueData as *const _)
|
||||
.ok_or_else(|| anyhow!("password was missing from keychain item"))?;
|
||||
.context("password was missing from keychain item")?;
|
||||
let password = CFType::wrap_under_get_rule(*password)
|
||||
.downcast::<CFData>()
|
||||
.ok_or_else(|| anyhow!("password was not a string"))?;
|
||||
.context("password was not a string")?;
|
||||
|
||||
Ok(Some((username.to_string(), password.bytes().to_vec())))
|
||||
}
|
||||
|
@ -1168,10 +1156,7 @@ impl Platform for MacPlatform {
|
|||
query_attrs.set(kSecAttrServer as *const _, url.as_CFTypeRef());
|
||||
|
||||
let status = SecItemDelete(query_attrs.as_concrete_TypeRef());
|
||||
|
||||
if status != errSecSuccess {
|
||||
return Err(anyhow!("delete password failed: {}", status));
|
||||
}
|
||||
anyhow::ensure!(status == errSecSuccess, "delete password failed: {status}");
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
|
@ -1455,15 +1440,12 @@ unsafe fn ns_string(string: &str) -> id {
|
|||
|
||||
unsafe fn ns_url_to_path(url: id) -> Result<PathBuf> {
|
||||
let path: *mut c_char = msg_send![url, fileSystemRepresentation];
|
||||
if path.is_null() {
|
||||
Err(anyhow!("url is not a file path: {}", unsafe {
|
||||
CStr::from_ptr(url.absoluteString().UTF8String()).to_string_lossy()
|
||||
}))
|
||||
} else {
|
||||
Ok(PathBuf::from(OsStr::from_bytes(unsafe {
|
||||
CStr::from_ptr(path).to_bytes()
|
||||
})))
|
||||
}
|
||||
anyhow::ensure!(!path.is_null(), "url is not a file path: {}", unsafe {
|
||||
CStr::from_ptr(url.absoluteString().UTF8String()).to_string_lossy()
|
||||
});
|
||||
Ok(PathBuf::from(OsStr::from_bytes(unsafe {
|
||||
CStr::from_ptr(path).to_bytes()
|
||||
})))
|
||||
}
|
||||
|
||||
#[link(name = "Carbon", kind = "framework")]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue