use std::sync::Arc;
use thiserror::Error;
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum Operation {
Get,
Set,
Backfill,
Remove,
Clear,
}
#[derive(Debug, Error)]
#[error("backend '{backend}' failed during {operation:?}: {source}")]
pub struct BackendFailure {
pub operation: Operation,
pub backend: Arc<str>,
pub source: Box<dyn std::error::Error + Send + Sync>,
}
#[derive(Debug, thiserror::Error)]
pub enum KapeError {
#[error(transparent)]
BackendSource {
source: Box<dyn std::error::Error + Send + Sync>,
},
#[error(transparent)]
Backend(BackendFailure),
#[error("loader failed: {source}")]
Loader {
source: Box<dyn std::error::Error + Send + Sync>,
},
#[error("invalid TTL {0}: expected -1, 0, or a positive millisecond value")]
InvalidTtl(i64),
#[error(
"duplicate batch key at index {duplicate_index}; first appeared at index {first_index}"
)]
DuplicateBatchKey {
first_index: usize,
duplicate_index: usize,
},
#[error("cache requires at least one backend")]
NoBackends,
#[error("backend name must not be empty")]
EmptyBackendName,
#[error("duplicate backend name '{0}'")]
DuplicateBackendName(String),
}
pub type KapeResult<T> = Result<T, KapeError>;
impl KapeError {
pub fn backend<E>(error: E) -> Self
where
E: std::error::Error + Send + Sync + 'static,
{
Self::BackendSource {
source: Box::new(error),
}
}
pub(crate) fn loader<E>(error: E) -> Self
where
E: std::error::Error + Send + Sync + 'static,
{
Self::Loader {
source: Box::new(error),
}
}
pub(crate) fn into_source(self) -> Box<dyn std::error::Error + Send + Sync> {
match self {
Self::BackendSource { source } => source,
error => Box::new(error),
}
}
}