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:
Kirill Bulatov 2025-05-21 02:06:07 +03:00 committed by GitHub
parent 1e51a7ac44
commit 16366cf9f2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
294 changed files with 2037 additions and 2610 deletions

View file

@ -1,4 +1,4 @@
use anyhow::{Result, anyhow};
use anyhow::Result;
use collections::{BTreeMap, HashMap, IndexMap};
use fs::Fs;
use gpui::{
@ -154,12 +154,12 @@ impl KeymapFile {
pub fn load_asset(asset_path: &str, cx: &App) -> anyhow::Result<Vec<KeyBinding>> {
match Self::load(asset_str::<SettingsAssets>(asset_path).as_ref(), cx) {
KeymapFileLoadResult::Success { key_bindings } => Ok(key_bindings),
KeymapFileLoadResult::SomeFailedToLoad { error_message, .. } => Err(anyhow!(
"Error loading built-in keymap \"{asset_path}\": {error_message}",
)),
KeymapFileLoadResult::JsonParseFailure { error } => Err(anyhow!(
"JSON parse error in built-in keymap \"{asset_path}\": {error}"
)),
KeymapFileLoadResult::SomeFailedToLoad { error_message, .. } => {
anyhow::bail!("Error loading built-in keymap \"{asset_path}\": {error_message}",)
}
KeymapFileLoadResult::JsonParseFailure { error } => {
anyhow::bail!("JSON parse error in built-in keymap \"{asset_path}\": {error}")
}
}
}
@ -173,14 +173,14 @@ impl KeymapFile {
key_bindings,
error_message,
..
} if key_bindings.is_empty() => Err(anyhow!(
"Error loading built-in keymap \"{asset_path}\": {error_message}",
)),
} if key_bindings.is_empty() => {
anyhow::bail!("Error loading built-in keymap \"{asset_path}\": {error_message}",)
}
KeymapFileLoadResult::Success { key_bindings, .. }
| KeymapFileLoadResult::SomeFailedToLoad { key_bindings, .. } => Ok(key_bindings),
KeymapFileLoadResult::JsonParseFailure { error } => Err(anyhow!(
"JSON parse error in built-in keymap \"{asset_path}\": {error}"
)),
KeymapFileLoadResult::JsonParseFailure { error } => {
anyhow::bail!("JSON parse error in built-in keymap \"{asset_path}\": {error}")
}
}
}

View file

@ -1,4 +1,4 @@
use anyhow::{Context as _, Result, anyhow};
use anyhow::{Context as _, Result};
use collections::{BTreeMap, HashMap, btree_map, hash_map};
use ec4rs::{ConfigParser, PropertiesSource, Section};
use fs::Fs;
@ -635,13 +635,10 @@ impl SettingsStore {
cx: &mut App,
) -> Result<()> {
let settings: Value = parse_json_with_comments(default_settings_content)?;
if settings.is_object() {
self.raw_default_settings = settings;
self.recompute_values(None, cx)?;
Ok(())
} else {
Err(anyhow!("settings must be an object"))
}
anyhow::ensure!(settings.is_object(), "settings must be an object");
self.raw_default_settings = settings;
self.recompute_values(None, cx)?;
Ok(())
}
/// Sets the user settings via a JSON string.