From da1c2592111947a41f1648cc3b34fbd0e5acd5b7 Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Sun, 26 Feb 2023 12:15:27 -0800 Subject: [PATCH] index_store: use custom error type for write errors Public APIs should use custom error types (not `io::Error` as here). The caller isn't affected by this commit because it just unwraps the error. --- lib/src/index_store.rs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/src/index_store.rs b/lib/src/index_store.rs index e4311c0f2..6d5f32033 100644 --- a/lib/src/index_store.rs +++ b/lib/src/index_store.rs @@ -21,6 +21,7 @@ use std::sync::Arc; use itertools::Itertools; use tempfile::NamedTempFile; +use thiserror::Error; use crate::backend::CommitId; use crate::commit::Commit; @@ -31,6 +32,12 @@ use crate::op_store::OperationId; use crate::operation::Operation; use crate::store::Store; +#[derive(Debug, Error)] +pub enum IndexWriteError { + #[error("{0}")] + Other(String), +} + pub struct IndexStore { dir: PathBuf, } @@ -79,9 +86,16 @@ impl IndexStore { &self, index: MutableIndex, op_id: &OperationId, - ) -> io::Result> { - let index = index.save_in(self.dir.clone())?; - self.associate_file_with_operation(&index, op_id)?; + ) -> Result, IndexWriteError> { + let index = index.save_in(self.dir.clone()).map_err(|err| { + IndexWriteError::Other(format!("Failed to write commit index file: {err:?}")) + })?; + self.associate_file_with_operation(&index, op_id) + .map_err(|err| { + IndexWriteError::Other(format!( + "Failed to associate commit index file with a operation {op_id:?}: {err:?}" + )) + })?; Ok(index) }