dodo/storage/
error.rs

1use std::{fmt, io, result};
2use std::error::Error;
3
4/// Storage error.
5#[derive(Debug)]
6pub struct StorageError(Box<RawStorageError>);
7
8#[derive(Debug)]
9struct RawStorageError {
10    kind: StorageErrorKind,
11    source: Option<Box<dyn std::error::Error + Send + Sync>>,
12}
13
14impl StorageError {
15    pub(crate) fn not_found() -> Self {
16        Self(Box::new(RawStorageError {
17            kind: StorageErrorKind::NotFound,
18            source: None,
19        }))
20    }
21
22    pub(crate) fn invalid_uuid<E>(error: E) -> Self
23        where E: Into<Box<dyn std::error::Error + Send + Sync>> {
24        Self(Box::new(RawStorageError {
25            kind: StorageErrorKind::InvalidUuid,
26            source: Some(error.into()),
27        }))
28    }
29
30    pub(crate) fn io<E>(error: E) -> Self
31        where E: Into<Box<dyn std::error::Error + Send + Sync>> {
32        Self(Box::new(RawStorageError {
33            kind: StorageErrorKind::Io,
34            source: Some(error.into()),
35        }))
36    }
37
38    pub(crate) fn other<E>(error: E) -> Self
39        where E: Into<Box<dyn std::error::Error + Send + Sync>> {
40        Self(Box::new(RawStorageError {
41            kind: StorageErrorKind::Other,
42            source: Some(error.into()),
43        }))
44    }
45
46    /// Error kind.
47    pub fn kind(&self) -> StorageErrorKind {
48        self.0.kind
49    }
50
51    /// Returns true if this error is NotFound.
52    pub fn is_not_found(&self) -> bool {
53        StorageErrorKind::NotFound == self.0.kind
54    }
55
56    /// Returns true if this error a InvalidUuid.
57    pub fn is_invalid_uuid(&self) -> bool {
58        StorageErrorKind::InvalidUuid == self.0.kind
59    }
60
61    /// Returns true if this error Io.
62    pub fn is_io(&self) -> bool {
63        StorageErrorKind::Io == self.0.kind
64    }
65
66    /// Returns true if this error is Other.
67    pub fn is_other(&self) -> bool {
68        StorageErrorKind::Other == self.0.kind
69    }
70}
71
72impl Error for StorageError {
73    fn source(&self) -> Option<&(dyn Error + 'static)> {
74        match self.0.source {
75            Some(ref source) => Some(&**source),
76            None => None
77        }
78    }
79}
80
81impl fmt::Display for StorageError {
82    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
83        self.0.kind.fmt(f)
84    }
85}
86
87impl From<uuid::Error> for StorageError {
88    fn from(error: uuid::Error) -> Self {
89        Self::invalid_uuid(error)
90    }
91}
92
93impl From<io::Error> for StorageError {
94    fn from(error: io::Error) -> Self {
95        Self::io(error)
96    }
97}
98
99/// Storage error kind.
100#[derive(Debug, Copy, Clone, Eq, PartialEq)]
101pub enum StorageErrorKind {
102    /// Entry not found, so the requested operation could not be performed on it.
103    NotFound,
104    /// Invalid Uuid encountered.
105    InvalidUuid,
106    /// Io error (unexpected EOF, interrupted, ...).
107    Io,
108    /// Other error.
109    Other,
110}
111
112impl fmt::Display for StorageErrorKind {
113    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
114        use StorageErrorKind::*;
115
116        match self {
117            NotFound => write!(f, "entry not found"),
118            InvalidUuid => write!(f, "invalid uuid"),
119            Io => write!(f, "io error"),
120            Other => write!(f, "other error"),
121        }
122    }
123}
124
125/// Storage result.
126pub type Result<T> = result::Result<T, StorageError>;