1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
use rocksdb::Error as RocksError;
use std::error::Error;
use std::fmt;
use std::io::Error as IOError;

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DatabaseError {
    NotFound,
    InvalidData,
    Internal(String),
}

impl From<IOError> for DatabaseError {
    fn from(err: IOError) -> Self {
        DatabaseError::Internal(err.to_string())
    }
}

impl From<RocksError> for DatabaseError {
    fn from(err: RocksError) -> Self {
        DatabaseError::Internal(err.to_string())
    }
}

impl Error for DatabaseError {}
impl fmt::Display for DatabaseError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let printable = match *self {
            DatabaseError::NotFound => "not found".to_owned(),
            DatabaseError::InvalidData => "invalid data".to_owned(),
            DatabaseError::Internal(ref err) => format!("internal error: {:?}", err),
        };
        write!(f, "{}", printable)
    }
}