use std::error::Error;
use std::fmt;
#[cfg(feature = "diesel")]
use crate::error::ConstraintViolationType;
use crate::error::{
ConstraintViolationError, InternalError, InvalidStateError, ResourceTemporarilyUnavailableError,
};
#[derive(Debug)]
pub enum RegistryError {
InternalError(InternalError),
ConstraintViolationError(ConstraintViolationError),
ResourceTemporarilyUnavailableError(ResourceTemporarilyUnavailableError),
InvalidStateError(InvalidStateError),
}
impl Error for RegistryError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
RegistryError::InternalError(err) => Some(err),
RegistryError::ConstraintViolationError(err) => Some(err),
RegistryError::ResourceTemporarilyUnavailableError(err) => Some(err),
RegistryError::InvalidStateError(err) => Some(err),
}
}
}
impl fmt::Display for RegistryError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
RegistryError::InternalError(err) => write!(f, "{}", err),
RegistryError::ConstraintViolationError(err) => write!(f, "{}", err),
RegistryError::ResourceTemporarilyUnavailableError(err) => {
write!(f, "{}", err)
}
RegistryError::InvalidStateError(err) => write!(f, "{}", err),
}
}
}
#[cfg(feature = "diesel")]
impl From<diesel::r2d2::PoolError> for RegistryError {
fn from(err: diesel::r2d2::PoolError) -> Self {
RegistryError::ResourceTemporarilyUnavailableError(
ResourceTemporarilyUnavailableError::from_source(Box::new(err)),
)
}
}
impl From<InternalError> for RegistryError {
fn from(err: InternalError) -> Self {
Self::InternalError(err)
}
}
#[derive(Debug)]
pub enum InvalidNodeError {
DuplicateEndpoint(String),
DuplicateIdentity(String),
EmptyEndpoint,
EmptyIdentity,
EmptyDisplayName,
EmptyKey,
InvalidIdentity(String, String),
MissingEndpoints,
MissingKeys,
}
impl Error for InvalidNodeError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
InvalidNodeError::DuplicateEndpoint(_) => None,
InvalidNodeError::DuplicateIdentity(_) => None,
InvalidNodeError::EmptyEndpoint => None,
InvalidNodeError::EmptyIdentity => None,
InvalidNodeError::EmptyDisplayName => None,
InvalidNodeError::EmptyKey => None,
InvalidNodeError::InvalidIdentity(..) => None,
InvalidNodeError::MissingEndpoints => None,
InvalidNodeError::MissingKeys => None,
}
}
}
impl fmt::Display for InvalidNodeError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
InvalidNodeError::DuplicateEndpoint(endpoint) => {
write!(f, "another node with endpoint {} exists", endpoint)
}
InvalidNodeError::DuplicateIdentity(identity) => {
write!(f, "another node with identity {} exists", identity)
}
InvalidNodeError::EmptyEndpoint => write!(f, "node endpoint cannot be empty"),
InvalidNodeError::EmptyIdentity => write!(f, "node must have non-empty identity"),
InvalidNodeError::EmptyDisplayName => {
write!(f, "node must have non-empty display_name")
}
InvalidNodeError::EmptyKey => write!(f, "node key cannot be empty"),
InvalidNodeError::InvalidIdentity(identity, msg) => {
write!(f, "identity {} is invalid: {}", identity, msg)
}
InvalidNodeError::MissingEndpoints => write!(f, "node must have one or more endpoints"),
InvalidNodeError::MissingKeys => write!(f, "node must have one or more keys"),
}
}
}
#[cfg(feature = "diesel")]
impl From<diesel::result::Error> for RegistryError {
fn from(err: diesel::result::Error) -> Self {
match err {
diesel::result::Error::DatabaseError(db_err_kind, _) => match db_err_kind {
diesel::result::DatabaseErrorKind::UniqueViolation => {
RegistryError::ConstraintViolationError(
ConstraintViolationError::from_source_with_violation_type(
ConstraintViolationType::Unique,
Box::new(err),
),
)
}
diesel::result::DatabaseErrorKind::ForeignKeyViolation => {
RegistryError::ConstraintViolationError(
ConstraintViolationError::from_source_with_violation_type(
ConstraintViolationType::ForeignKey,
Box::new(err),
),
)
}
_ => RegistryError::InternalError(InternalError::from_source(Box::new(err))),
},
_ => RegistryError::InternalError(InternalError::from_source(Box::new(err))),
}
}
}