use common::coordinator::WriteError;
use common::{SequenceError, StorageError};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Error {
Storage(String),
Encoding(String),
InvalidInput(String),
Internal(String),
}
impl std::error::Error for Error {}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::Storage(msg) => write!(f, "Storage error: {}", msg),
Error::Encoding(msg) => write!(f, "Encoding error: {}", msg),
Error::InvalidInput(msg) => write!(f, "Invalid input: {}", msg),
Error::Internal(msg) => write!(f, "Internal error: {}", msg),
}
}
}
impl From<StorageError> for Error {
fn from(err: StorageError) -> Self {
match err {
StorageError::Storage(msg) => Error::Storage(msg),
StorageError::Internal(msg) => Error::Internal(msg),
}
}
}
impl From<SequenceError> for Error {
fn from(err: SequenceError) -> Self {
match err {
SequenceError::Storage(storage_err) => Error::from(storage_err),
SequenceError::Deserialize(de_err) => Error::Encoding(de_err.message),
}
}
}
impl From<WriteError> for Error {
fn from(err: WriteError) -> Self {
match err {
WriteError::Backpressure => Error::Internal("write queue full".into()),
WriteError::Shutdown => Error::Internal("coordinator shut down".into()),
WriteError::ApplyError(_, msg) => Error::Internal(msg),
WriteError::FlushError(msg) => Error::Storage(msg),
WriteError::Internal(msg) => Error::Internal(msg),
}
}
}
impl From<&str> for Error {
fn from(msg: &str) -> Self {
Error::InvalidInput(msg.to_string())
}
}
pub type Result<T> = std::result::Result<T, Error>;