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
|
@ -4,7 +4,7 @@
|
|||
mod reliability;
|
||||
mod zed;
|
||||
|
||||
use anyhow::{Context as _, Result, anyhow};
|
||||
use anyhow::{Context as _, Result};
|
||||
use clap::{Parser, command};
|
||||
use cli::FORCE_CLI_MODE_ENV_VAR_NAME;
|
||||
use client::{Client, ProxySettings, UserStore, parse_zed_link};
|
||||
|
@ -1073,7 +1073,7 @@ fn parse_url_arg(arg: &str, cx: &App) -> Result<String> {
|
|||
{
|
||||
Ok(arg.into())
|
||||
} else {
|
||||
Err(anyhow!("error parsing path argument: {}", error))
|
||||
anyhow::bail!("error parsing path argument: {error}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -421,7 +421,7 @@ fn initialize_panels(
|
|||
workspace.update_in(cx, |workspace, window, cx| {
|
||||
workspace.add_panel(debug_panel, window, cx);
|
||||
})?;
|
||||
Result::<_, anyhow::Error>::Ok(())
|
||||
anyhow::Ok(())
|
||||
},
|
||||
)
|
||||
.detach()
|
||||
|
|
|
@ -951,7 +951,7 @@ impl SerializableItem for ComponentPreview {
|
|||
item_id: ItemId,
|
||||
window: &mut Window,
|
||||
cx: &mut App,
|
||||
) -> Task<gpui::Result<Entity<Self>>> {
|
||||
) -> Task<anyhow::Result<Entity<Self>>> {
|
||||
let deserialized_active_page =
|
||||
match COMPONENT_PREVIEW_DB.get_active_page(item_id, workspace_id) {
|
||||
Ok(page) => {
|
||||
|
@ -1009,7 +1009,7 @@ impl SerializableItem for ComponentPreview {
|
|||
alive_items: Vec<ItemId>,
|
||||
_window: &mut Window,
|
||||
cx: &mut App,
|
||||
) -> Task<gpui::Result<()>> {
|
||||
) -> Task<anyhow::Result<()>> {
|
||||
delete_unloaded_items(
|
||||
alive_items,
|
||||
workspace_id,
|
||||
|
@ -1026,7 +1026,7 @@ impl SerializableItem for ComponentPreview {
|
|||
_closing: bool,
|
||||
_window: &mut Window,
|
||||
cx: &mut Context<Self>,
|
||||
) -> Option<Task<gpui::Result<()>>> {
|
||||
) -> Option<Task<anyhow::Result<()>>> {
|
||||
let active_page = self.active_page_id(cx);
|
||||
let workspace_id = self.workspace_id?;
|
||||
Some(cx.background_spawn(async move {
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use crate::handle_open_request;
|
||||
use crate::restorable_workspace_locations;
|
||||
use anyhow::{Context as _, Result, anyhow};
|
||||
use anyhow::{Context as _, Result};
|
||||
use cli::{CliRequest, CliResponse, ipc::IpcSender};
|
||||
use cli::{IpcHandshake, ipc};
|
||||
use client::parse_zed_link;
|
||||
|
@ -74,13 +74,14 @@ impl OpenRequest {
|
|||
let url = url::Url::parse(file)?;
|
||||
let host = url
|
||||
.host()
|
||||
.ok_or_else(|| anyhow!("missing host in ssh url: {}", file))?
|
||||
.with_context(|| format!("missing host in ssh url: {file}"))?
|
||||
.to_string();
|
||||
let username = Some(url.username().to_string()).filter(|s| !s.is_empty());
|
||||
let port = url.port();
|
||||
if !self.open_paths.is_empty() {
|
||||
return Err(anyhow!("cannot open both local and ssh paths"));
|
||||
}
|
||||
anyhow::ensure!(
|
||||
self.open_paths.is_empty(),
|
||||
"cannot open both local and ssh paths"
|
||||
);
|
||||
let mut connection_options = SshSettings::get_global(cx).connection_options_for(
|
||||
host.clone(),
|
||||
port,
|
||||
|
@ -90,9 +91,10 @@ impl OpenRequest {
|
|||
connection_options.password = Some(password.to_string());
|
||||
}
|
||||
if let Some(ssh_connection) = &self.ssh_connection {
|
||||
if *ssh_connection != connection_options {
|
||||
return Err(anyhow!("cannot open multiple ssh connections"));
|
||||
}
|
||||
anyhow::ensure!(
|
||||
*ssh_connection == connection_options,
|
||||
"cannot open multiple ssh connections"
|
||||
);
|
||||
}
|
||||
self.ssh_connection = Some(connection_options);
|
||||
self.parse_file_path(url.path());
|
||||
|
@ -123,7 +125,7 @@ impl OpenRequest {
|
|||
}
|
||||
}
|
||||
}
|
||||
Err(anyhow!("invalid zed url: {}", request_path))
|
||||
anyhow::bail!("invalid zed url: {request_path}")
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -141,7 +143,7 @@ impl OpenListener {
|
|||
pub fn open_urls(&self, urls: Vec<String>) {
|
||||
self.0
|
||||
.unbounded_send(urls)
|
||||
.map_err(|_| anyhow!("no listener for open requests"))
|
||||
.context("no listener for open requests")
|
||||
.log_err();
|
||||
}
|
||||
}
|
||||
|
@ -191,7 +193,7 @@ fn connect_to_cli(
|
|||
break;
|
||||
}
|
||||
}
|
||||
Ok::<_, anyhow::Error>(())
|
||||
anyhow::Ok(())
|
||||
});
|
||||
|
||||
Ok((async_request_rx, response_tx))
|
||||
|
@ -401,9 +403,7 @@ async fn open_workspaces(
|
|||
}
|
||||
}
|
||||
|
||||
if errored {
|
||||
return Err(anyhow!("failed to open a workspace"));
|
||||
}
|
||||
anyhow::ensure!(!errored, "failed to open a workspace");
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue