watchexec/src/error.rs

89 lines
2.4 KiB
Rust
Raw Normal View History

use std::{error::Error as StdError, fmt, io, sync::PoisonError};
pub type Result<T> = ::std::result::Result<T, Error>;
pub enum Error {
Canonicalization(String, io::Error),
Clap(clap::Error),
Glob(globset::Error),
Io(io::Error),
Notify(notify::Error),
PoisonedLock,
}
2018-09-09 00:38:07 +02:00
impl StdError for Error {
fn description(&self) -> &str {
2018-09-09 00:38:07 +02:00
// This method is soft-deprecated and shouldn't be used,
// see Display for the actual description.
"a watchexec error"
}
}
impl From<clap::Error> for Error {
fn from(err: clap::Error) -> Self {
2019-10-27 12:06:09 +01:00
Self::Clap(err)
}
}
impl From<globset::Error> for Error {
fn from(err: globset::Error) -> Self {
2019-10-27 12:06:09 +01:00
Self::Glob(err)
}
}
impl From<io::Error> for Error {
fn from(err: io::Error) -> Self {
Self::Io(
match err.raw_os_error() {
Some(os_err) => match os_err {
7 => {
let msg = "There are so many changed files that the environment variables of the child process have been overrun. Try running with --no-meta or --no-environment.";
io::Error::new(io::ErrorKind::Other, msg)
},
_ => err,
}
None => err,
}
)
}
}
impl From<notify::Error> for Error {
fn from(err: notify::Error) -> Self {
match err {
2019-10-27 12:06:09 +01:00
notify::Error::Io(err) => Self::Io(err),
other => Self::Notify(other),
}
}
}
impl<'a, T> From<PoisonError<T>> for Error {
fn from(_err: PoisonError<T>) -> Self {
2019-10-27 12:06:09 +01:00
Self::PoisonedLock
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let (error_type, error) = match self {
2019-10-27 12:31:52 +01:00
Self::Canonicalization(path, err) => (
"Path",
format!("couldn't canonicalize '{}':\n{}", path, err),
),
2019-10-27 12:06:09 +01:00
Self::Clap(err) => ("Argument", err.to_string()),
Self::Glob(err) => ("Globset", err.to_string()),
Self::Io(err) => ("I/O", err.to_string()),
Self::Notify(err) => ("Notify", err.to_string()),
Self::PoisonedLock => ("Internal", "poisoned lock".to_string()),
};
write!(f, "{} error: {}", error_type, error)
}
}
impl fmt::Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}