refactor: reduce map_err boilerplate with context helper methods (TODO-025)

Add per-crate variant_ctx() helper methods to StorageError, BlobCacheError,
ProtocolError, and BridgeError. Replace 78 .map_err(|e| format!(...))
closures with concise context calls. Identical error messages preserved.
This commit is contained in:
Edison Jwa
2026-06-11 12:25:00 +09:00
parent 1efeaac19d
commit 40883c40c8
4 changed files with 89 additions and 49 deletions
+14 -8
View File
@@ -29,6 +29,12 @@ pub enum BlobCacheError {
InvalidKey(String),
}
impl BlobCacheError {
fn io_ctx(ctx: impl std::fmt::Display, e: impl std::fmt::Display) -> Self {
BlobCacheError::Io(format!("{ctx}: {e}"))
}
}
/// Content-addressed blob cache backed by cacache.
pub struct BlobCache {
cache_dir: PathBuf,
@@ -51,7 +57,7 @@ impl BlobCache {
// cacache creates the directory on first write, but we create
// it eagerly so total_size() works before any writes.
std::fs::create_dir_all(&cache_dir)
.map_err(|e| BlobCacheError::Io(format!("mkdir cache: {e}")))?;
.map_err(|e| BlobCacheError::io_ctx("mkdir cache", e))?;
Ok(Self {
cache_dir,
max_bytes,
@@ -73,7 +79,7 @@ impl BlobCache {
let cache_key = format!("{prefix}{key}");
cacache::write(&self.cache_dir, &cache_key, data)
.await
.map_err(|e| BlobCacheError::Io(format!("cacache write: {e}")))?;
.map_err(|e| BlobCacheError::io_ctx("cacache write", e))?;
Ok(())
}
@@ -106,7 +112,7 @@ impl BlobCache {
let cache_key = format!("{prefix}{key}");
cacache::remove(&self.cache_dir, &cache_key)
.await
.map_err(|e| BlobCacheError::Io(format!("cacache remove: {e}")))?;
.map_err(|e| BlobCacheError::io_ctx("cacache remove", e))?;
Ok(())
}
@@ -116,14 +122,14 @@ impl BlobCache {
tokio::task::spawn_blocking(move || {
if path.exists() {
std::fs::remove_dir_all(&path)
.map_err(|e| BlobCacheError::Io(format!("clear cache: {e}")))?;
.map_err(|e| BlobCacheError::io_ctx("clear cache", e))?;
std::fs::create_dir_all(&path)
.map_err(|e| BlobCacheError::Io(format!("recreate cache dir: {e}")))?;
.map_err(|e| BlobCacheError::io_ctx("recreate cache dir", e))?;
}
Ok(())
})
.await
.map_err(|e| BlobCacheError::Io(format!("clear task: {e}")))?
.map_err(|e| BlobCacheError::io_ctx("clear task", e))?
}
/// Return total bytes used by all blobs.
@@ -148,7 +154,7 @@ impl BlobCache {
Ok(total)
})
.await
.map_err(|e| BlobCacheError::Io(format!("total_size task: {e}")))?
.map_err(|e| BlobCacheError::io_ctx("total_size task", e))?
}
/// Evict oldest entries by timestamp until total size is under
@@ -198,7 +204,7 @@ impl BlobCache {
Ok(())
})
.await
.map_err(|e| BlobCacheError::Io(format!("evict task: {e}")))?
.map_err(|e| BlobCacheError::io_ctx("evict task", e))?
}
}