From 9aa2009320abc3755f234f27c4b0f11f55982a4a Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Mon, 23 May 2022 15:29:29 -0700 Subject: [PATCH] working_copy: improve error handling when getting file stats --- lib/src/working_copy.rs | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/lib/src/working_copy.rs b/lib/src/working_copy.rs index 31e6a6070..33941dd2d 100644 --- a/lib/src/working_copy.rs +++ b/lib/src/working_copy.rs @@ -201,8 +201,12 @@ pub struct CheckoutStats { #[derive(Debug, Error)] pub enum SnapshotError { - #[error("Failed to open file {path}: {err:?}")] - FileOpenError { path: PathBuf, err: std::io::Error }, + #[error("{message}: {err:?}")] + IoError { + message: String, + #[source] + err: std::io::Error, + }, #[error("Working copy path {} is not valid UTF-8", path.to_string_lossy())] InvalidUtf8Path { path: OsString }, #[error("Symlink {path} target is not valid UTF-8")] @@ -338,8 +342,8 @@ impl TreeState { path: &RepoPath, disk_path: &Path, ) -> Result { - let file = File::open(disk_path).map_err(|err| SnapshotError::FileOpenError { - path: disk_path.to_path_buf(), + let file = File::open(disk_path).map_err(|err| SnapshotError::IoError { + message: format!("Failed to open file {}", disk_path.display()), err, })?; Ok(self.store.write_file(path, &mut Box::new(file))?) @@ -352,8 +356,8 @@ impl TreeState { ) -> Result { let target = disk_path .read_link() - .map_err(|err| SnapshotError::FileOpenError { - path: disk_path.to_path_buf(), + .map_err(|err| SnapshotError::IoError { + message: format!("Failed to read symlink {}", disk_path.display()), err, })?; let str_target = @@ -461,7 +465,12 @@ impl TreeState { // ignore it. return Ok(()); } - let metadata = disk_path.symlink_metadata().unwrap(); + let metadata = disk_path + .symlink_metadata() + .map_err(|err| SnapshotError::IoError { + message: format!("Failed to stat file {}", disk_path.display()), + err, + })?; let mut new_file_state = file_state(&metadata); match maybe_current_file_state { None => {