improve error

This commit is contained in:
Robin Appelman 2025-06-09 18:21:56 +02:00
commit b3c36dbdf5
2 changed files with 16 additions and 4 deletions

View file

@ -55,18 +55,29 @@ mod listener;
pub use listener::ShortcutListener;
/// Error emitted when an input device can't be opened
#[derive(Debug, Clone)]
#[derive(Debug)]
#[non_exhaustive]
pub struct DeviceOpenError {
pub device: PathBuf,
pub error: std::io::Error,
}
impl Display for DeviceOpenError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "Failed to open device {:?}", self.device)
write!(
f,
"Failed to open device {}: {:#}",
self.device.display(),
self.error
)
}
}
impl std::error::Error for DeviceOpenError {}
impl std::error::Error for DeviceOpenError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(&self.error)
}
}
/// Modifier key for shortcuts
#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq, Display, FromStr)]

View file

@ -52,8 +52,9 @@ impl ShortcutListener {
.iter()
.map(|path| {
let path = path.as_ref();
let res = Device::open(path).map_err(|_| DeviceOpenError {
let res = Device::open(path).map_err(|error| DeviceOpenError {
device: path.into(),
error,
});
debug!(device = ?path, success = res.is_ok(), "opening input device");
res