Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhanced error reporting #98

Merged
merged 3 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading