use std::fmt;
#[derive(Debug)]
pub enum CacheError<E> {
Inner(E),
}
impl<E: fmt::Display> fmt::Display for CacheError<E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
CacheError::Inner(e) => write!(f, "inner service error: {}", e),
}
}
}
impl<E: std::error::Error + 'static> std::error::Error for CacheError<E> {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
CacheError::Inner(e) => Some(e),
}
}
}
impl<E> CacheError<E> {
pub fn into_inner(self) -> E {
match self {
CacheError::Inner(e) => e,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_inner_error() {
let err = CacheError::Inner("test error");
assert_eq!(err.to_string(), "inner service error: test error");
assert_eq!(err.into_inner(), "test error");
}
}