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,6 +1,6 @@
|
|||
use std::{collections::HashMap, path::PathBuf, sync::OnceLock};
|
||||
|
||||
use anyhow::Result;
|
||||
use anyhow::{Context as _, Result};
|
||||
use async_trait::async_trait;
|
||||
use dap::adapters::{DebugTaskDefinition, latest_github_release};
|
||||
use futures::StreamExt;
|
||||
|
@ -69,22 +69,16 @@ impl CodeLldbDebugAdapter {
|
|||
let arch = match std::env::consts::ARCH {
|
||||
"aarch64" => "arm64",
|
||||
"x86_64" => "x64",
|
||||
_ => {
|
||||
return Err(anyhow!(
|
||||
"unsupported architecture {}",
|
||||
std::env::consts::ARCH
|
||||
));
|
||||
unsupported => {
|
||||
anyhow::bail!("unsupported architecture {unsupported}");
|
||||
}
|
||||
};
|
||||
let platform = match std::env::consts::OS {
|
||||
"macos" => "darwin",
|
||||
"linux" => "linux",
|
||||
"windows" => "win32",
|
||||
_ => {
|
||||
return Err(anyhow!(
|
||||
"unsupported operating system {}",
|
||||
std::env::consts::OS
|
||||
));
|
||||
unsupported => {
|
||||
anyhow::bail!("unsupported operating system {unsupported}");
|
||||
}
|
||||
};
|
||||
let asset_name = format!("codelldb-{platform}-{arch}.vsix");
|
||||
|
@ -94,7 +88,7 @@ impl CodeLldbDebugAdapter {
|
|||
.assets
|
||||
.iter()
|
||||
.find(|asset| asset.name == asset_name)
|
||||
.ok_or_else(|| anyhow!("no asset found matching {:?}", asset_name))?
|
||||
.with_context(|| format!("no asset found matching {asset_name:?}"))?
|
||||
.browser_download_url
|
||||
.clone(),
|
||||
};
|
||||
|
@ -138,10 +132,7 @@ impl DebugAdapter for CodeLldbDebugAdapter {
|
|||
version_path
|
||||
} else {
|
||||
let mut paths = delegate.fs().read_dir(&adapter_path).await?;
|
||||
paths
|
||||
.next()
|
||||
.await
|
||||
.ok_or_else(|| anyhow!("No adapter found"))??
|
||||
paths.next().await.context("No adapter found")??
|
||||
};
|
||||
let adapter_dir = version_path.join("extension").join("adapter");
|
||||
let path = adapter_dir.join("codelldb").to_string_lossy().to_string();
|
||||
|
|
|
@ -8,7 +8,7 @@ mod ruby;
|
|||
|
||||
use std::sync::Arc;
|
||||
|
||||
use anyhow::{Result, anyhow};
|
||||
use anyhow::Result;
|
||||
use async_trait::async_trait;
|
||||
use codelldb::CodeLldbDebugAdapter;
|
||||
use dap::{
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use std::{collections::HashMap, ffi::OsStr};
|
||||
|
||||
use anyhow::{Result, bail};
|
||||
use anyhow::{Context as _, Result, bail};
|
||||
use async_trait::async_trait;
|
||||
use dap::{StartDebuggingRequestArguments, adapters::DebugTaskDefinition};
|
||||
use gpui::AsyncApp;
|
||||
|
@ -78,7 +78,7 @@ impl DebugAdapter for GdbDebugAdapter {
|
|||
.which(OsStr::new("gdb"))
|
||||
.await
|
||||
.and_then(|p| p.to_str().map(|s| s.to_string()))
|
||||
.ok_or(anyhow!("Could not find gdb in path"));
|
||||
.context("Could not find gdb in path");
|
||||
|
||||
if gdb_path.is_err() && user_setting_path.is_none() {
|
||||
bail!("Could not find gdb path or it's not installed");
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
use anyhow::Context as _;
|
||||
use dap::{StartDebuggingRequestArguments, adapters::DebugTaskDefinition};
|
||||
use gpui::{AsyncApp, SharedString};
|
||||
use language::LanguageName;
|
||||
|
@ -59,18 +60,14 @@ impl DebugAdapter for GoDebugAdapter {
|
|||
.which(OsStr::new("dlv"))
|
||||
.await
|
||||
.and_then(|p| p.to_str().map(|p| p.to_string()))
|
||||
.ok_or(anyhow!("Dlv not found in path"))?;
|
||||
.context("Dlv not found in path")?;
|
||||
|
||||
let tcp_connection = config.tcp_connection.clone().unwrap_or_default();
|
||||
let (host, port, timeout) = crate::configure_tcp_connection(tcp_connection).await?;
|
||||
|
||||
Ok(DebugAdapterBinary {
|
||||
command: delve_path,
|
||||
arguments: vec![
|
||||
"dap".into(),
|
||||
"--listen".into(),
|
||||
format!("{}:{}", host, port),
|
||||
],
|
||||
arguments: vec!["dap".into(), "--listen".into(), format!("{host}:{port}")],
|
||||
cwd: None,
|
||||
envs: HashMap::default(),
|
||||
connection: Some(adapters::TcpArguments {
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
use adapters::latest_github_release;
|
||||
use anyhow::Context as _;
|
||||
use dap::{StartDebuggingRequestArguments, adapters::DebugTaskDefinition};
|
||||
use gpui::AsyncApp;
|
||||
use std::{collections::HashMap, path::PathBuf, sync::OnceLock};
|
||||
|
@ -74,7 +75,7 @@ impl JsDebugAdapter {
|
|||
.assets
|
||||
.iter()
|
||||
.find(|asset| asset.name == asset_name)
|
||||
.ok_or_else(|| anyhow!("no asset found matching {:?}", asset_name))?
|
||||
.with_context(|| format!("no asset found matching {asset_name:?}"))?
|
||||
.browser_download_url
|
||||
.clone(),
|
||||
})
|
||||
|
@ -98,7 +99,7 @@ impl JsDebugAdapter {
|
|||
file_name.starts_with(&file_name_prefix)
|
||||
})
|
||||
.await
|
||||
.ok_or_else(|| anyhow!("Couldn't find JavaScript dap directory"))?
|
||||
.context("Couldn't find JavaScript dap directory")?
|
||||
};
|
||||
|
||||
let tcp_connection = config.tcp_connection.clone().unwrap_or_default();
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
use adapters::latest_github_release;
|
||||
use anyhow::Context as _;
|
||||
use dap::adapters::{DebugTaskDefinition, TcpArguments};
|
||||
use gpui::{AsyncApp, SharedString};
|
||||
use language::LanguageName;
|
||||
|
@ -58,7 +59,7 @@ impl PhpDebugAdapter {
|
|||
.assets
|
||||
.iter()
|
||||
.find(|asset| asset.name == asset_name)
|
||||
.ok_or_else(|| anyhow!("no asset found matching {:?}", asset_name))?
|
||||
.with_context(|| format!("no asset found matching {asset_name:?}"))?
|
||||
.browser_download_url
|
||||
.clone(),
|
||||
})
|
||||
|
@ -82,7 +83,7 @@ impl PhpDebugAdapter {
|
|||
file_name.starts_with(&file_name_prefix)
|
||||
})
|
||||
.await
|
||||
.ok_or_else(|| anyhow!("Couldn't find PHP dap directory"))?
|
||||
.context("Couldn't find PHP dap directory")?
|
||||
};
|
||||
|
||||
let tcp_connection = config.tcp_connection.clone().unwrap_or_default();
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
use crate::*;
|
||||
use anyhow::Context as _;
|
||||
use dap::{DebugRequest, StartDebuggingRequestArguments, adapters::DebugTaskDefinition};
|
||||
use gpui::{AsyncApp, SharedString};
|
||||
use language::LanguageName;
|
||||
|
@ -112,7 +113,7 @@ impl PythonDebugAdapter {
|
|||
file_name.starts_with(&file_name_prefix)
|
||||
})
|
||||
.await
|
||||
.ok_or_else(|| anyhow!("Debugpy directory not found"))?
|
||||
.context("Debugpy directory not found")?
|
||||
};
|
||||
|
||||
let toolchain = delegate
|
||||
|
@ -143,7 +144,7 @@ impl PythonDebugAdapter {
|
|||
};
|
||||
|
||||
Ok(DebugAdapterBinary {
|
||||
command: python_path.ok_or(anyhow!("failed to find binary path for python"))?,
|
||||
command: python_path.context("failed to find binary path for Python")?,
|
||||
arguments: vec![
|
||||
debugpy_dir
|
||||
.join(Self::ADAPTER_PATH)
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use anyhow::{Result, anyhow};
|
||||
use anyhow::Result;
|
||||
use async_trait::async_trait;
|
||||
use dap::{
|
||||
DebugRequest, StartDebuggingRequestArguments,
|
||||
|
@ -54,12 +54,11 @@ impl DebugAdapter for RubyDebugAdapter {
|
|||
.arg("debug")
|
||||
.output()
|
||||
.await?;
|
||||
if !output.status.success() {
|
||||
return Err(anyhow!(
|
||||
"Failed to install rdbg:\n{}",
|
||||
String::from_utf8_lossy(&output.stderr).to_string()
|
||||
));
|
||||
}
|
||||
anyhow::ensure!(
|
||||
output.status.success(),
|
||||
"Failed to install rdbg:\n{}",
|
||||
String::from_utf8_lossy(&output.stderr).to_string()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue