bunbun/src/error.rs

56 lines
1.6 KiB
Rust
Raw Normal View History

2019-12-29 05:08:13 +00:00
use std::error::Error;
2019-12-26 22:01:45 +00:00
use std::fmt;
#[derive(Debug)]
pub enum BunBunError {
2020-07-05 00:44:30 +00:00
Io(std::io::Error),
Parse(serde_yaml::Error),
Watch(hotwatch::Error),
LoggerInit(log::SetLoggerError),
CustomProgram(String),
2020-07-05 00:41:55 +00:00
NoValidConfigPath,
InvalidConfigPath(std::path::PathBuf, std::io::Error),
2020-07-05 16:17:29 +00:00
ConfigTooLarge(u64),
ZeroByteConfig,
2020-09-28 04:51:02 +00:00
JsonParse(serde_json::Error),
2019-12-26 22:01:45 +00:00
}
2019-12-29 05:08:13 +00:00
impl Error for BunBunError {}
2019-12-26 22:01:45 +00:00
impl fmt::Display for BunBunError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
2020-07-05 00:44:30 +00:00
Self::Io(e) => e.fmt(f),
Self::Parse(e) => e.fmt(f),
Self::Watch(e) => e.fmt(f),
Self::LoggerInit(e) => e.fmt(f),
Self::CustomProgram(msg) => write!(f, "{}", msg),
Self::NoValidConfigPath => write!(f, "No valid config path was found!"),
2020-07-05 00:41:55 +00:00
Self::InvalidConfigPath(path, reason) => {
write!(f, "Failed to access {:?}: {}", path, reason)
}
2020-07-05 16:17:29 +00:00
Self::ConfigTooLarge(size) => write!(f, "The config file was too large ({} bytes)! Pass in --large-config to bypass this check.", size),
2020-09-28 04:51:02 +00:00
Self::ZeroByteConfig => write!(f, "The config provided reported a size of 0 bytes. Please check your config path!"),
Self::JsonParse(e) => e.fmt(f),
2019-12-26 22:01:45 +00:00
}
}
}
/// Generates a from implementation from the specified type to the provided
/// bunbun error.
macro_rules! from_error {
($from:ty, $to:ident) => {
impl From<$from> for BunBunError {
fn from(e: $from) -> Self {
2020-01-01 00:42:53 +00:00
Self::$to(e)
2019-12-26 22:01:45 +00:00
}
}
};
}
2020-07-05 00:44:30 +00:00
from_error!(std::io::Error, Io);
from_error!(serde_yaml::Error, Parse);
from_error!(hotwatch::Error, Watch);
from_error!(log::SetLoggerError, LoggerInit);
2020-09-28 04:51:02 +00:00
from_error!(serde_json::Error, JsonParse);