indradb/
errors.rs

1use std::error::Error as StdError;
2use std::fmt;
3use std::io::Error as IoError;
4use std::result::Result as StdResult;
5
6#[cfg(feature = "rocksdb-datastore")]
7use bincode::Error as BincodeError;
8use rmp_serde::encode::Error as RmpEncodeError;
9#[cfg(feature = "rocksdb-datastore")]
10use rocksdb::Error as RocksDbError;
11use serde_json::Error as JsonError;
12
13/// An error triggered by the datastore.
14#[non_exhaustive]
15#[derive(Debug)]
16pub enum Error {
17    /// The requested UUID is already taken.
18    UuidTaken,
19
20    /// An error occurred in the underlying datastore.
21    Datastore(Box<dyn StdError + Send + Sync>),
22
23    /// A generic I/O error occurred.
24    Io(IoError),
25
26    /// A query occurred on a property that isn't indexed.
27    NotIndexed,
28
29    /// For functionality that isn't supported.
30    Unsupported,
31
32    /// A validation error occurred.
33    Invalid(ValidationError),
34
35    /// The operation cannot work with the given query, based off it's output
36    /// type (e.g. attempting to delete using a query that outputs a count.)
37    OperationOnQuery,
38}
39
40impl StdError for Error {
41    fn source(&self) -> Option<&(dyn StdError + 'static)> {
42        match *self {
43            Error::Datastore(ref err) => Some(&**err),
44            Error::Io(ref err) => Some(err),
45            Error::Invalid(ref err) => Some(err),
46            _ => None,
47        }
48    }
49}
50
51impl fmt::Display for Error {
52    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
53        match *self {
54            Error::UuidTaken => write!(f, "UUID already taken"),
55            Error::Datastore(ref err) => write!(f, "error in the underlying datastore: {err}"),
56            Error::Io(ref err) => write!(f, "I/O error: {err}"),
57            Error::NotIndexed => write!(f, "query attempted on a property that isn't indexed"),
58            Error::Unsupported => write!(f, "functionality not supported"),
59            Error::Invalid(ref err) => write!(f, "{err}"),
60            Error::OperationOnQuery => write!(f, "the operation cannot work with the given query"),
61        }
62    }
63}
64
65impl From<JsonError> for Error {
66    fn from(err: JsonError) -> Self {
67        Error::Datastore(Box::new(err))
68    }
69}
70
71impl From<IoError> for Error {
72    fn from(err: IoError) -> Self {
73        Error::Io(err)
74    }
75}
76
77#[cfg(feature = "rocksdb-datastore")]
78impl From<BincodeError> for Error {
79    fn from(err: BincodeError) -> Self {
80        Error::Datastore(Box::new(err))
81    }
82}
83
84impl From<RmpEncodeError> for Error {
85    fn from(err: RmpEncodeError) -> Self {
86        Error::Datastore(Box::new(err))
87    }
88}
89
90#[cfg(feature = "rocksdb-datastore")]
91impl From<RocksDbError> for Error {
92    fn from(err: RocksDbError) -> Self {
93        Error::Datastore(Box::new(err))
94    }
95}
96
97impl From<ValidationError> for Error {
98    fn from(err: ValidationError) -> Self {
99        Error::Invalid(err)
100    }
101}
102
103/// A result that might be an `Error`.
104pub type Result<T> = StdResult<T, Error>;
105
106/// A validation error
107#[derive(Debug)]
108pub enum ValidationError {
109    /// The value is invalid.
110    InvalidValue,
111    /// The value is too long.
112    ValueTooLong,
113    /// The input UUID is the maximum value, and cannot be incremented.
114    CannotIncrementUuid,
115    /// The given query combination cannot be nested (e.g. attempting to build
116    /// a query that gets vertex properties from a query that outputs a
117    /// count.)
118    InnerQuery,
119}
120
121impl StdError for ValidationError {}
122
123impl fmt::Display for ValidationError {
124    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
125        match *self {
126            ValidationError::InvalidValue => write!(f, "invalid value"),
127            ValidationError::ValueTooLong => write!(f, "value too long"),
128            ValidationError::CannotIncrementUuid => write!(f, "could not increment the UUID"),
129            ValidationError::InnerQuery => write!(f, "the given query combination cannot be nested"),
130        }
131    }
132}
133
134/// A result that might be a `ValidationError`.
135pub type ValidationResult<T> = StdResult<T, ValidationError>;