Cleanup some error reporting

This commit is contained in:
Lukas Wirth 2025-08-14 11:04:31 +02:00
parent eb086e3689
commit 3758e8d663
7 changed files with 27 additions and 46 deletions

View file

@ -333,7 +333,7 @@ impl NativeAgent {
Err(err) => (
None,
Some(RulesLoadingError {
message: format!("{err}").into(),
message: format!("{err:#}").into(),
}),
),
};

View file

@ -557,7 +557,7 @@ impl ChannelStore {
};
cx.background_spawn(async move {
task.await.map_err(|error| {
anyhow!("{error}").context(format!("failed to open channel {resource_name}"))
anyhow!(error).context(format!("failed to open channel {resource_name}"))
})
})
}

View file

@ -686,7 +686,7 @@ impl Client {
}
ConnectionResult::Result(r) => {
if let Err(error) = r {
log::error!("failed to connect: {error}");
log::error!("failed to connect: {error:?}");
} else {
break;
}

View file

@ -874,7 +874,7 @@ fn operation_from_storage(
_format_version: i32,
) -> Result<proto::operation::Variant, Error> {
let operation =
storage::Operation::decode(row.value.as_slice()).map_err(|error| anyhow!("{error}"))?;
storage::Operation::decode(row.value.as_slice()).map_err(|error| anyhow!(error))?;
let version = version_from_storage(&operation.version);
Ok(if operation.is_undo {
proto::operation::Variant::Undo(proto::operation::Undo {

View file

@ -1312,7 +1312,7 @@ pub async fn handle_metrics(Extension(server): Extension<Arc<Server>>) -> Result
let metric_families = prometheus::gather();
let encoded_metrics = encoder
.encode_to_string(&metric_families)
.map_err(|err| anyhow!("{err}"))?;
.map_err(|err| anyhow!(err))?;
Ok(encoded_metrics)
}

View file

@ -1,5 +1,5 @@
use crate::*;
use anyhow::Context as _;
use anyhow::{Context as _, anyhow, bail};
use dap::{DebugRequest, StartDebuggingRequestArguments, adapters::DebugTaskDefinition};
use fs::RemoveOptions;
use futures::{StreamExt, TryStreamExt};
@ -24,7 +24,7 @@ use util::{ResultExt, maybe};
#[derive(Default)]
pub(crate) struct PythonDebugAdapter {
debugpy_whl_base_path: OnceCell<Result<Arc<Path>, String>>,
debugpy_whl_base_path: OnceCell<Arc<Path>>,
}
impl PythonDebugAdapter {
@ -91,13 +91,13 @@ impl PythonDebugAdapter {
})
}
async fn fetch_wheel(delegate: &Arc<dyn DapDelegate>) -> Result<Arc<Path>, String> {
async fn fetch_wheel(delegate: &Arc<dyn DapDelegate>) -> Result<Arc<Path>> {
let system_python = Self::system_python_name(delegate)
.await
.ok_or_else(|| String::from("Could not find a Python installation"))?;
.ok_or_else(|| anyhow!("Could not find a Python installation"))?;
let command: &OsStr = system_python.as_ref();
let download_dir = debug_adapters_dir().join(Self::ADAPTER_NAME).join("wheels");
std::fs::create_dir_all(&download_dir).map_err(|e| e.to_string())?;
std::fs::create_dir_all(&download_dir)?;
let installation_succeeded = util::command::new_smol_command(command)
.args([
"-m",
@ -109,32 +109,27 @@ impl PythonDebugAdapter {
download_dir.to_string_lossy().as_ref(),
])
.output()
.await
.map_err(|e| format!("{e}"))?
.await?
.status
.success();
if !installation_succeeded {
return Err("debugpy installation failed".into());
bail!("debugpy installation failed");
}
let wheel_path = std::fs::read_dir(&download_dir)
.map_err(|e| e.to_string())?
let wheel_path = std::fs::read_dir(&download_dir)?
.find_map(|entry| {
entry.ok().filter(|e| {
e.file_type().is_ok_and(|typ| typ.is_file())
&& Path::new(&e.file_name()).extension() == Some("whl".as_ref())
})
})
.ok_or_else(|| String::from("Did not find a .whl in {download_dir}"))?;
.ok_or_else(|| anyhow!("Did not find a .whl in {download_dir:?}"))?;
util::archive::extract_zip(
&debug_adapters_dir().join(Self::ADAPTER_NAME),
File::open(&wheel_path.path())
.await
.map_err(|e| e.to_string())?,
File::open(&wheel_path.path()).await?,
)
.await
.map_err(|e| e.to_string())?;
.await?;
Ok(Arc::from(wheel_path.path()))
}
@ -198,20 +193,17 @@ impl PythonDebugAdapter {
.await;
}
async fn fetch_debugpy_whl(
&self,
delegate: &Arc<dyn DapDelegate>,
) -> Result<Arc<Path>, String> {
async fn fetch_debugpy_whl(&self, delegate: &Arc<dyn DapDelegate>) -> Arc<Path> {
self.debugpy_whl_base_path
.get_or_init(|| async move {
Self::maybe_fetch_new_wheel(delegate).await;
Ok(Arc::from(
Arc::from(
debug_adapters_dir()
.join(Self::ADAPTER_NAME)
.join("debugpy")
.join("adapter")
.as_ref(),
))
)
})
.await
.clone()
@ -704,10 +696,7 @@ impl DebugAdapter for PythonDebugAdapter {
)
.await;
let debugpy_path = self
.fetch_debugpy_whl(delegate)
.await
.map_err(|e| anyhow::anyhow!("{e}"))?;
let debugpy_path = self.fetch_debugpy_whl(delegate).await;
if let Some(toolchain) = &toolchain {
log::debug!(
"Found debugpy in toolchain environment: {}",

View file

@ -1,4 +1,4 @@
use anyhow::{Context as _, ensure};
use anyhow::{Context as _, Error, ensure};
use anyhow::{Result, anyhow};
use async_trait::async_trait;
use collections::HashMap;
@ -952,7 +952,7 @@ impl pet_core::os_environment::Environment for EnvironmentApi<'_> {
}
pub(crate) struct PyLspAdapter {
python_venv_base: OnceCell<Result<Arc<Path>, String>>,
python_venv_base: OnceCell<Result<Arc<Path>, Arc<Error>>>,
}
impl PyLspAdapter {
const SERVER_NAME: LanguageServerName = LanguageServerName::new_static("pylsp");
@ -994,13 +994,9 @@ impl PyLspAdapter {
None
}
async fn base_venv(&self, delegate: &dyn LspAdapterDelegate) -> Result<Arc<Path>, String> {
async fn base_venv(&self, delegate: &dyn LspAdapterDelegate) -> Result<Arc<Path>, Arc<Error>> {
self.python_venv_base
.get_or_init(move || async move {
Self::ensure_venv(delegate)
.await
.map_err(|e| format!("{e}"))
})
.get_or_init(move || async move { Self::ensure_venv(delegate).await.map_err(Arc::new) })
.await
.clone()
}
@ -1254,7 +1250,7 @@ impl LspAdapter for PyLspAdapter {
}
pub(crate) struct BasedPyrightLspAdapter {
python_venv_base: OnceCell<Result<Arc<Path>, String>>,
python_venv_base: OnceCell<Result<Arc<Path>, Arc<Error>>>,
}
impl BasedPyrightLspAdapter {
@ -1301,13 +1297,9 @@ impl BasedPyrightLspAdapter {
None
}
async fn base_venv(&self, delegate: &dyn LspAdapterDelegate) -> Result<Arc<Path>, String> {
async fn base_venv(&self, delegate: &dyn LspAdapterDelegate) -> Result<Arc<Path>, Arc<Error>> {
self.python_venv_base
.get_or_init(move || async move {
Self::ensure_venv(delegate)
.await
.map_err(|e| format!("{e}"))
})
.get_or_init(move || async move { Self::ensure_venv(delegate).await.map_err(Arc::new) })
.await
.clone()
}