cache/
error.rs

1//! Cache error types.
2
3use std::sync::Arc;
4
5/// A result type returning cache errors.
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// A result type returning reference counted cache errors.
9///
10/// Stores an [`Arc<Error>`] since the error will be stuck inside a
11/// [`OnceCell`](once_cell::sync::OnceCell) and cannot be owned without cloning.
12pub type ArcResult<T> = std::result::Result<T, Arc<Error>>;
13
14/// The error type for cache functions.
15#[derive(thiserror::Error, Debug)]
16pub enum Error {
17    #[allow(missing_docs)]
18    #[error(transparent)]
19    Io(#[from] std::io::Error),
20    #[allow(missing_docs)]
21    #[error(transparent)]
22    Transport(#[from] tonic::transport::Error),
23    #[allow(missing_docs)]
24    #[error(transparent)]
25    TokioRusqlite(#[from] tokio_rusqlite::Error),
26    #[allow(missing_docs)]
27    #[error(transparent)]
28    Rusqlite(#[from] rusqlite::Error),
29    #[allow(missing_docs)]
30    #[error(transparent)]
31    Toml(#[from] toml::de::Error),
32    #[allow(missing_docs)]
33    #[error(transparent)]
34    Flexbuffers(#[from] flexbuffers::DeserializationError),
35    #[allow(missing_docs)]
36    #[error(transparent)]
37    Rpc(#[from] Box<tonic::Status>),
38    #[allow(missing_docs)]
39    #[error(transparent)]
40    Join(#[from] tokio::task::JoinError),
41    /// The user-provided generator panicked.
42    #[error("generator panicked")]
43    Panic,
44    /// Exponential backoff for polling the server failed.
45    #[error(transparent)]
46    Backoff(#[from] Box<backoff::Error<Error>>),
47    /// The desired cache entry is currently being loaded.
48    #[error("entry is currently being loaded")]
49    EntryLoading,
50    /// The desired cache entry is currently unavailable.
51    #[error("entry is currently unavailable")]
52    EntryUnavailable,
53    /// The desired cache entry cannot be assigned.
54    #[error("entry cannot be assigned")]
55    EntryUnassignable,
56}
57
58/// The error type returned by [`CacheHandle::try_inner`](crate::CacheHandle::try_inner).
59pub enum TryInnerError<'a, E> {
60    /// An error thrown by the cache.
61    CacheError(Arc<Error>),
62    /// An error thrown by the generator.
63    GeneratorError(&'a E),
64}
65
66impl<E> From<Arc<Error>> for TryInnerError<'_, E> {
67    fn from(value: Arc<Error>) -> Self {
68        Self::CacheError(value)
69    }
70}
71
72impl<'a, E> From<&'a E> for TryInnerError<'a, E> {
73    fn from(value: &'a E) -> Self {
74        Self::GeneratorError(value)
75    }
76}