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:
@@ -1,4 +1,4 @@
|
||||
use anyhow::{Result, anyhow};
|
||||
use anyhow::{Context as _, Result};
|
||||
use async_trait::async_trait;
|
||||
use dap::{DapLocator, DebugRequest, adapters::DebugAdapterName};
|
||||
use gpui::SharedString;
|
||||
@@ -90,11 +90,10 @@ impl DapLocator for CargoLocator {
|
||||
}
|
||||
|
||||
async fn run(&self, build_config: SpawnInTerminal) -> Result<DebugRequest> {
|
||||
let Some(cwd) = build_config.cwd.clone() else {
|
||||
return Err(anyhow!(
|
||||
"Couldn't get cwd from debug config which is needed for locators"
|
||||
));
|
||||
};
|
||||
let cwd = build_config
|
||||
.cwd
|
||||
.clone()
|
||||
.context("Couldn't get cwd from debug config which is needed for locators")?;
|
||||
let builder = ShellBuilder::new(true, &build_config.shell).non_interactive();
|
||||
let (program, args) = builder.build(
|
||||
"cargo".into(),
|
||||
@@ -119,9 +118,7 @@ impl DapLocator for CargoLocator {
|
||||
}
|
||||
|
||||
let status = child.status().await?;
|
||||
if !status.success() {
|
||||
return Err(anyhow::anyhow!("Cargo command failed"));
|
||||
}
|
||||
anyhow::ensure!(status.success(), "Cargo command failed");
|
||||
|
||||
let executables = output
|
||||
.lines()
|
||||
@@ -133,9 +130,10 @@ impl DapLocator for CargoLocator {
|
||||
.map(String::from)
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
if executables.is_empty() {
|
||||
return Err(anyhow!("Couldn't get executable in cargo locator"));
|
||||
};
|
||||
anyhow::ensure!(
|
||||
!executables.is_empty(),
|
||||
"Couldn't get executable in cargo locator"
|
||||
);
|
||||
let is_test = build_config.args.first().map_or(false, |arg| arg == "test");
|
||||
|
||||
let mut test_name = None;
|
||||
@@ -161,7 +159,7 @@ impl DapLocator for CargoLocator {
|
||||
};
|
||||
|
||||
let Some(executable) = executable.or_else(|| executables.first().cloned()) else {
|
||||
return Err(anyhow!("Couldn't get executable in cargo locator"));
|
||||
anyhow::bail!("Couldn't get executable in cargo locator");
|
||||
};
|
||||
|
||||
let args = test_name.into_iter().collect();
|
||||
|
||||
Reference in New Issue
Block a user