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,7 +1,7 @@
|
|||
//! Module for managing breakpoints in a project.
|
||||
//!
|
||||
//! Breakpoints are separate from a session because they're not associated with any particular debug session. They can also be set up without a session running.
|
||||
use anyhow::{Result, anyhow};
|
||||
use anyhow::{Context as _, Result};
|
||||
pub use breakpoints_in_file::{BreakpointSessionState, BreakpointWithPosition};
|
||||
use breakpoints_in_file::{BreakpointsInFile, StatefulBreakpoint};
|
||||
use collections::{BTreeMap, HashMap};
|
||||
|
@ -219,7 +219,7 @@ impl BreakpointStore {
|
|||
})
|
||||
.ok()
|
||||
.flatten()
|
||||
.ok_or_else(|| anyhow!("Invalid project path"))?
|
||||
.context("Invalid project path")?
|
||||
.await?;
|
||||
|
||||
breakpoints.update(&mut cx, move |this, cx| {
|
||||
|
@ -272,25 +272,25 @@ impl BreakpointStore {
|
|||
.update(&mut cx, |this, cx| {
|
||||
this.project_path_for_absolute_path(message.payload.path.as_ref(), cx)
|
||||
})?
|
||||
.ok_or_else(|| anyhow!("Could not resolve provided abs path"))?;
|
||||
.context("Could not resolve provided abs path")?;
|
||||
let buffer = this
|
||||
.update(&mut cx, |this, cx| {
|
||||
this.buffer_store().read(cx).get_by_path(&path, cx)
|
||||
})?
|
||||
.ok_or_else(|| anyhow!("Could not find buffer for a given path"))?;
|
||||
.context("Could not find buffer for a given path")?;
|
||||
let breakpoint = message
|
||||
.payload
|
||||
.breakpoint
|
||||
.ok_or_else(|| anyhow!("Breakpoint not present in RPC payload"))?;
|
||||
.context("Breakpoint not present in RPC payload")?;
|
||||
let position = language::proto::deserialize_anchor(
|
||||
breakpoint
|
||||
.position
|
||||
.clone()
|
||||
.ok_or_else(|| anyhow!("Anchor not present in RPC payload"))?,
|
||||
.context("Anchor not present in RPC payload")?,
|
||||
)
|
||||
.ok_or_else(|| anyhow!("Anchor deserialization failed"))?;
|
||||
let breakpoint = Breakpoint::from_proto(breakpoint)
|
||||
.ok_or_else(|| anyhow!("Could not deserialize breakpoint"))?;
|
||||
.context("Anchor deserialization failed")?;
|
||||
let breakpoint =
|
||||
Breakpoint::from_proto(breakpoint).context("Could not deserialize breakpoint")?;
|
||||
|
||||
breakpoints.update(&mut cx, |this, cx| {
|
||||
this.toggle_breakpoint(
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use anyhow::{Ok, Result, anyhow};
|
||||
use anyhow::{Context as _, Ok, Result};
|
||||
use dap::{
|
||||
Capabilities, ContinueArguments, ExceptionFilterOptions, InitializeRequestArguments,
|
||||
InitializeRequestArgumentsPathFormat, NextArguments, SetVariableResponse, SourceBreakpoint,
|
||||
|
@ -1766,7 +1766,7 @@ impl DapCommand for LocationsCommand {
|
|||
source: response
|
||||
.source
|
||||
.map(<dap::Source as ProtoConversion>::from_proto)
|
||||
.ok_or_else(|| anyhow!("Missing `source` field in Locations proto"))?,
|
||||
.context("Missing `source` field in Locations proto")?,
|
||||
line: response.line,
|
||||
column: response.column,
|
||||
end_line: response.end_line,
|
||||
|
|
|
@ -237,9 +237,7 @@ impl DapStore {
|
|||
let binary = DebugAdapterBinary::from_proto(response)?;
|
||||
let mut ssh_command = ssh_client.update(cx, |ssh, _| {
|
||||
anyhow::Ok(SshCommand {
|
||||
arguments: ssh
|
||||
.ssh_args()
|
||||
.ok_or_else(|| anyhow!("SSH arguments not found"))?,
|
||||
arguments: ssh.ssh_args().context("SSH arguments not found")?,
|
||||
})
|
||||
})??;
|
||||
|
||||
|
@ -316,10 +314,10 @@ impl DapStore {
|
|||
return Ok(result);
|
||||
}
|
||||
|
||||
Err(anyhow!(
|
||||
anyhow::bail!(
|
||||
"None of the locators for task `{}` completed successfully",
|
||||
build_command.label
|
||||
))
|
||||
)
|
||||
})
|
||||
} else {
|
||||
Task::ready(Err(anyhow!(
|
||||
|
@ -735,7 +733,7 @@ impl DapStore {
|
|||
let task = envelope
|
||||
.payload
|
||||
.build_command
|
||||
.ok_or_else(|| anyhow!("missing definition"))?;
|
||||
.context("missing definition")?;
|
||||
let build_task = SpawnInTerminal::from_proto(task);
|
||||
let locator = envelope.payload.locator;
|
||||
let request = this
|
||||
|
@ -753,10 +751,7 @@ impl DapStore {
|
|||
mut cx: AsyncApp,
|
||||
) -> Result<proto::DebugAdapterBinary> {
|
||||
let definition = DebugTaskDefinition::from_proto(
|
||||
envelope
|
||||
.payload
|
||||
.definition
|
||||
.ok_or_else(|| anyhow!("missing definition"))?,
|
||||
envelope.payload.definition.context("missing definition")?,
|
||||
)?;
|
||||
let (tx, mut rx) = mpsc::unbounded();
|
||||
let session_id = envelope.payload.session_id;
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -12,7 +12,7 @@ use super::dap_command::{
|
|||
TerminateThreadsCommand, ThreadsCommand, VariablesCommand,
|
||||
};
|
||||
use super::dap_store::DapStore;
|
||||
use anyhow::{Result, anyhow};
|
||||
use anyhow::{Context as _, Result, anyhow};
|
||||
use collections::{HashMap, HashSet, IndexMap, IndexSet};
|
||||
use dap::adapters::{DebugAdapterBinary, DebugAdapterName};
|
||||
use dap::messages::Response;
|
||||
|
@ -487,8 +487,7 @@ impl Mode {
|
|||
match self {
|
||||
Mode::Running(debug_adapter_client) => debug_adapter_client.request(request),
|
||||
Mode::Building => Task::ready(Err(anyhow!(
|
||||
"no adapter running to send request: {:?}",
|
||||
request
|
||||
"no adapter running to send request: {request:?}"
|
||||
))),
|
||||
}
|
||||
}
|
||||
|
@ -1736,7 +1735,7 @@ impl Session {
|
|||
anyhow::Ok(
|
||||
task.await
|
||||
.map(|response| response.targets)
|
||||
.ok_or_else(|| anyhow!("failed to fetch completions"))?,
|
||||
.context("failed to fetch completions")?,
|
||||
)
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue