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:
Kirill Bulatov 2025-05-21 02:06:07 +03:00 committed by GitHub
parent 1e51a7ac44
commit 16366cf9f2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
294 changed files with 2037 additions and 2610 deletions

View file

@ -10,7 +10,7 @@ use std::{any::Any, ops::ControlFlow, path::PathBuf, sync::Arc, time::Duration};
use crate::persistence::{self, DebuggerPaneItem, SerializedLayout};
use super::DebugPanelItemEvent;
use anyhow::{Result, anyhow};
use anyhow::{Context as _, Result, anyhow};
use breakpoint_list::BreakpointList;
use collections::{HashMap, IndexMap};
use console::Console;
@ -817,7 +817,7 @@ impl RunningState {
let exit_status = terminal
.read_with(cx, |terminal, cx| terminal.wait_for_completed_task(cx))?
.await
.ok_or_else(|| anyhow!("Failed to wait for completed task"))?;
.context("Failed to wait for completed task")?;
if !exit_status.success() {
anyhow::bail!("Build failed");
@ -829,22 +829,22 @@ impl RunningState {
let request = if let Some(request) = request {
request
} else if let Some((task, locator_name)) = build_output {
let locator_name = locator_name
.ok_or_else(|| anyhow!("Could not find a valid locator for a build task"))?;
let locator_name =
locator_name.context("Could not find a valid locator for a build task")?;
dap_store
.update(cx, |this, cx| {
this.run_debug_locator(&locator_name, task, cx)
})?
.await?
} else {
return Err(anyhow!("No request or build provided"));
anyhow::bail!("No request or build provided");
};
let request = match request {
dap::DebugRequest::Launch(launch_request) => {
let cwd = match launch_request.cwd.as_deref().and_then(|path| path.to_str()) {
Some(cwd) => {
let substituted_cwd = substitute_variables_in_str(&cwd, &task_context)
.ok_or_else(|| anyhow!("Failed to substitute variables in cwd"))?;
.context("substituting variables in cwd")?;
Some(PathBuf::from(substituted_cwd))
}
None => None,
@ -854,7 +854,7 @@ impl RunningState {
&launch_request.env.into_iter().collect(),
&task_context,
)
.ok_or_else(|| anyhow!("Failed to substitute variables in env"))?
.context("substituting variables in env")?
.into_iter()
.collect();
let new_launch_request = LaunchRequest {
@ -862,13 +862,13 @@ impl RunningState {
&launch_request.program,
&task_context,
)
.ok_or_else(|| anyhow!("Failed to substitute variables in program"))?,
.context("substituting variables in program")?,
args: launch_request
.args
.into_iter()
.map(|arg| substitute_variables_in_str(&arg, &task_context))
.collect::<Option<Vec<_>>>()
.ok_or_else(|| anyhow!("Failed to substitute variables in args"))?,
.context("substituting variables in args")?,
cwd,
env,
};
@ -994,7 +994,7 @@ impl RunningState {
.pty_info
.pid()
.map(|pid| pid.as_u32())
.ok_or_else(|| anyhow!("Terminal was spawned but PID was not available"))
.context("Terminal was spawned but PID was not available")
})?
});