Skip to main content

xet_client/chunk_cache/
error.rs

1use std::array::TryFromSliceError;
2use std::str::Utf8Error;
3
4use base64::DecodeError;
5use thiserror::Error;
6use tokio::task::JoinError;
7use xet_core_structures::merklehash::DataHashBytesParseError;
8
9#[derive(Debug, Error)]
10pub enum ChunkCacheError {
11    #[error("General: {0}")]
12    General(String),
13    #[error("IO: {0}")]
14    IO(#[from] std::io::Error),
15    #[error("ParseError: {0}")]
16    Parse(String),
17    #[error("bad range")]
18    BadRange,
19    #[error("cache is empty when it is presumed no empty")]
20    CacheEmpty,
21    #[error("Infallible")]
22    Infallible,
23    #[error("LockPoison")]
24    LockPoison,
25    #[error("invalid arguments")]
26    InvalidArguments,
27
28    #[error("RuntimeError")]
29    RuntimeError(#[from] JoinError),
30}
31
32impl ChunkCacheError {
33    pub fn parse<T: ToString>(value: T) -> ChunkCacheError {
34        ChunkCacheError::Parse(value.to_string())
35    }
36
37    pub fn general<T: ToString>(value: T) -> ChunkCacheError {
38        ChunkCacheError::General(value.to_string())
39    }
40}
41
42impl<T> From<std::sync::PoisonError<T>> for ChunkCacheError {
43    fn from(_value: std::sync::PoisonError<T>) -> Self {
44        ChunkCacheError::LockPoison
45    }
46}
47
48macro_rules! impl_parse_error_from_error {
49    ($error_type:ty) => {
50        impl From<$error_type> for ChunkCacheError {
51            fn from(value: $error_type) -> Self {
52                ChunkCacheError::parse(value)
53            }
54        }
55    };
56}
57
58impl_parse_error_from_error!(TryFromSliceError);
59impl_parse_error_from_error!(DecodeError);
60impl_parse_error_from_error!(DataHashBytesParseError);
61impl_parse_error_from_error!(Utf8Error);