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
|
@ -1,5 +1,5 @@
|
|||
use ::proto::{FromProto, ToProto};
|
||||
use anyhow::{Result, anyhow};
|
||||
use anyhow::{Context as _, Result, anyhow};
|
||||
|
||||
use extension::ExtensionHostProxy;
|
||||
use extension_host::headless_host::HeadlessExtensionStore;
|
||||
|
@ -368,7 +368,7 @@ impl HeadlessProject {
|
|||
let mut parent = path
|
||||
.parent()
|
||||
.ok_or(e)
|
||||
.map_err(|_| anyhow!("{:?} does not exist", path))?;
|
||||
.with_context(|| format!("{path:?} does not exist"))?;
|
||||
if parent == Path::new("") {
|
||||
parent = util::paths::home_dir();
|
||||
}
|
||||
|
@ -558,11 +558,7 @@ impl HeadlessProject {
|
|||
mut cx: AsyncApp,
|
||||
) -> Result<proto::FindSearchCandidatesResponse> {
|
||||
let message = envelope.payload;
|
||||
let query = SearchQuery::from_proto(
|
||||
message
|
||||
.query
|
||||
.ok_or_else(|| anyhow!("missing query field"))?,
|
||||
)?;
|
||||
let query = SearchQuery::from_proto(message.query.context("missing query field")?)?;
|
||||
let results = this.update(&mut cx, |this, cx| {
|
||||
this.buffer_store.update(cx, |buffer_store, cx| {
|
||||
buffer_store.find_search_candidates(&query, message.limit as _, this.fs.clone(), cx)
|
||||
|
|
|
@ -333,7 +333,7 @@ fn start_server(
|
|||
break;
|
||||
};
|
||||
if let Err(error) = incoming_tx.unbounded_send(message) {
|
||||
log::error!("failed to send message to application: {:?}. exiting.", error);
|
||||
log::error!("failed to send message to application: {error:?}. exiting.");
|
||||
return Err(anyhow!(error));
|
||||
}
|
||||
}
|
||||
|
@ -390,8 +390,7 @@ fn init_paths() -> anyhow::Result<()> {
|
|||
]
|
||||
.iter()
|
||||
{
|
||||
std::fs::create_dir_all(path)
|
||||
.map_err(|e| anyhow!("Could not create directory {:?}: {}", path, e))?;
|
||||
std::fs::create_dir_all(path).with_context(|| format!("creating directory {path:?}"))?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
@ -542,7 +541,7 @@ pub fn execute_proxy(identifier: String, is_reconnecting: bool) -> Result<()> {
|
|||
if is_reconnecting {
|
||||
if !server_running {
|
||||
log::error!("attempted to reconnect, but no server running");
|
||||
return Err(anyhow!(ProxyLaunchError::ServerNotRunning));
|
||||
anyhow::bail!(ProxyLaunchError::ServerNotRunning);
|
||||
}
|
||||
} else {
|
||||
if let Some(pid) = server_pid {
|
||||
|
@ -573,19 +572,20 @@ pub fn execute_proxy(identifier: String, is_reconnecting: bool) -> Result<()> {
|
|||
let mut stream = smol::net::unix::UnixStream::connect(&server_paths.stderr_socket).await?;
|
||||
let mut stderr_buffer = vec![0; 2048];
|
||||
loop {
|
||||
match stream.read(&mut stderr_buffer).await {
|
||||
Ok(0) => {
|
||||
match stream
|
||||
.read(&mut stderr_buffer)
|
||||
.await
|
||||
.context("reading stderr")?
|
||||
{
|
||||
0 => {
|
||||
let error =
|
||||
std::io::Error::new(std::io::ErrorKind::UnexpectedEof, "stderr closed");
|
||||
Err(anyhow!(error))?;
|
||||
}
|
||||
Ok(n) => {
|
||||
n => {
|
||||
stderr.write_all(&mut stderr_buffer[..n]).await?;
|
||||
stderr.flush().await?;
|
||||
}
|
||||
Err(error) => {
|
||||
Err(anyhow!("error reading stderr: {error:?}"))?;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
@ -868,7 +868,7 @@ fn read_proxy_settings(cx: &mut Context<HeadlessProject>) -> Option<Url> {
|
|||
}
|
||||
|
||||
fn daemonize() -> Result<ControlFlow<()>> {
|
||||
match fork::fork().map_err(|e| anyhow::anyhow!("failed to call fork with error code {}", e))? {
|
||||
match fork::fork().map_err(|e| anyhow!("failed to call fork with error code {e}"))? {
|
||||
fork::Fork::Parent(_) => {
|
||||
return Ok(ControlFlow::Break(()));
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue