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

@ -11,7 +11,7 @@ pub mod core_media {
CMItemIndex, CMSampleTimingInfo, CMTime, CMTimeMake, CMVideoCodecType,
kCMSampleAttachmentKey_NotSync, kCMTimeInvalid, kCMVideoCodecType_H264,
};
use anyhow::{Result, anyhow};
use anyhow::Result;
use core_foundation::{
array::{CFArray, CFArrayRef},
base::{CFTypeID, OSStatus, TCFType},
@ -69,12 +69,11 @@ pub mod core_media {
index as CMItemIndex,
&mut timing_info,
);
if result == 0 {
Ok(timing_info)
} else {
Err(anyhow!("error getting sample timing info, code {}", result))
}
anyhow::ensure!(
result == 0,
"error getting sample timing info, code {result}"
);
Ok(timing_info)
}
}
@ -153,11 +152,8 @@ pub mod core_media {
ptr::null_mut(),
ptr::null_mut(),
);
if result == 0 {
Ok(std::slice::from_raw_parts(bytes, len))
} else {
Err(anyhow!("error getting parameter set, code: {}", result))
}
anyhow::ensure!(result == 0, "error getting parameter set, code: {result}");
Ok(std::slice::from_raw_parts(bytes, len))
}
}
}
@ -231,7 +227,7 @@ pub mod core_video {
kCVPixelFormatType_32BGRA, kCVPixelFormatType_420YpCbCr8BiPlanarFullRange,
kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange, kCVPixelFormatType_420YpCbCr8Planar,
};
use anyhow::{Result, anyhow};
use anyhow::Result;
use core_foundation::{
base::kCFAllocatorDefault, dictionary::CFDictionaryRef, mach_port::CFAllocatorRef,
};
@ -267,11 +263,11 @@ pub mod core_video {
&mut this,
)
};
if result == kCVReturnSuccess {
unsafe { Ok(CVMetalTextureCache::wrap_under_create_rule(this)) }
} else {
Err(anyhow!("could not create texture cache, code: {}", result))
}
anyhow::ensure!(
result == kCVReturnSuccess,
"could not create texture cache, code: {result}"
);
unsafe { Ok(CVMetalTextureCache::wrap_under_create_rule(this)) }
}
/// # Safety
@ -300,11 +296,11 @@ pub mod core_video {
&mut this,
)
};
if result == kCVReturnSuccess {
unsafe { Ok(CVMetalTexture::wrap_under_create_rule(this)) }
} else {
Err(anyhow!("could not create texture, code: {}", result))
}
anyhow::ensure!(
result == kCVReturnSuccess,
"could not create texture, code: {result}"
);
unsafe { Ok(CVMetalTexture::wrap_under_create_rule(this)) }
}
}