use mio_extras::channel::TrySendError;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("Bad parameter: {reason}")]
BadParameter { reason: String },
#[error("Unsupported operation")]
Unsupported,
#[error("Out of resources")]
OutOfResources,
#[error("Entity not yet enabled")]
NotEnabled,
#[error("Attempted to modify immutable entity")]
ImmutablePolicy,
#[error("Inconsistent policies: {reason}")]
InconsistentPolicy { reason: String },
#[error("Precondition not met: {precondition}")]
PreconditionNotMet { precondition: String },
#[error("Illegal operation: {reason}")]
IllegalOperation { reason: String },
#[error("Lock poisoned")]
LockPoisoned,
#[error("Internal error: {reason}")]
Internal { reason: String },
#[error(transparent)]
Io(#[from] std::io::Error),
#[error("Serialization error: {reason}")]
Serialization { reason: String },
#[error("Discovery error: {reason}")]
Discovery { reason: String },
}
impl Error {
pub fn bad_parameter<T>(reason: impl Into<String>) -> Result<T> {
Err(Self::BadParameter {
reason: reason.into(),
})
}
pub fn precondition_not_met<T>(precondition: impl Into<String>) -> Result<T> {
Err(Self::PreconditionNotMet {
precondition: precondition.into(),
})
}
}
#[doc(hidden)]
#[macro_export]
macro_rules! log_and_err_precondition_not_met {
($err_msg:literal) => {{
log::error!($err_msg);
Error::precondition_not_met($err_msg)
}};
}
#[doc(hidden)]
#[macro_export]
macro_rules! log_and_err_internal {
($($arg:tt)*) => (
{ log::error!($($arg)*);
Err( Error::Internal{ reason: format!($($arg)*) } )
}
)
}
#[doc(hidden)]
#[macro_export]
macro_rules! log_and_err_discovery {
($($arg:tt)*) => (
{ error!($($arg)*);
Error::Message(format!($($arg)*) )
}
)
}
impl<T> From<std::sync::PoisonError<T>> for Error {
fn from(_e: std::sync::PoisonError<T>) -> Self {
Self::LockPoisoned
}
}
impl<T> From<TrySendError<T>> for Error
where
TrySendError<T>: std::error::Error,
{
fn from(e: TrySendError<T>) -> Self {
Self::Internal {
reason: format!("Cannot send to internal mio channel: {:?}", e),
}
}
}