Skip to content

Commit

Permalink
feat: improved error granularity
Browse files Browse the repository at this point in the history
Inform about operating-system specific errors more clearly, thus avoid degenerating error information.
  • Loading branch information
Byron authored Feb 5, 2024
2 parents d7abb5b + 2b1c9fa commit 452be83
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ pub enum Error {
description: String,
},

Os {
code: i32,
description: String,
},

/// **freedesktop only**
///
/// Error coming from file system
Expand Down
21 changes: 14 additions & 7 deletions src/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,20 @@ fn delete_using_finder(full_paths: Vec<String>) -> Result<(), Error> {
let result = command.output().map_err(into_unknown)?;
if !result.status.success() {
let stderr = String::from_utf8_lossy(&result.stderr);
return Err(Error::Unknown {
description: format!(
"The AppleScript exited with error. Error code: {:?}, stderr: {}",
result.status.code(),
stderr
),
});
match result.status.code() {
None => {
return Err(Error::Unknown {
description: format!("The AppleScript exited with error. stderr: {}", stderr),
})
}

Some(code) => {
return Err(Error::Os {
code,
description: format!("The AppleScript exited with error. stderr: {}", stderr),
})
}
};
}
Ok(())
}
Expand Down
20 changes: 17 additions & 3 deletions src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{Error, TrashContext, TrashItem, TrashItemMetadata, TrashItemSize};
use std::{
borrow::Borrow,
ffi::{c_void, OsStr, OsString},
fs,
fs, io,
os::windows::{ffi::OsStrExt, prelude::*},
path::PathBuf,
};
Expand Down Expand Up @@ -34,7 +34,7 @@ const FOFX_EARLYFAILURE: u32 = 0x00100000;

impl From<windows::core::Error> for Error {
fn from(err: windows::core::Error) -> Error {
Error::Unknown { description: format!("windows error: {err}") }
Error::Os { code: err.code().0, description: format!("windows error: {err}") }
}
}

Expand Down Expand Up @@ -316,7 +316,21 @@ fn traverse_paths_recursively(
continue;
}

for entry in fs::read_dir(&base_path).map_err(|err| Error::Unknown { description: err.to_string() })? {
let entries = match fs::read_dir(&base_path) {
Ok(entries) => entries,
Err(err) => {
let err = match err.kind() {
io::ErrorKind::NotFound | io::ErrorKind::PermissionDenied => {
Error::CouldNotAccess { target: base_path.to_string_lossy().to_string() }
}
_ => Error::Unknown { description: err.to_string() },
};

return Err(err);
}
};

for entry in entries {
let entry = entry.map_err(|err| Error::Unknown { description: err.to_string() })?;
traverse_paths_recursively(Some(entry.path()), collection)?;
}
Expand Down

0 comments on commit 452be83

Please sign in to comment.