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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//! # Errors for storage of `Keyfiles`

use keystore::SerializeError;
use rocksdb;
use serde_json;

use std::{error, fmt, io, str};

///
#[derive(Debug)]
pub enum KeystoreError {
    /// General storage error
    StorageError(String),

    /// `KeyFile` not found
    NotFound(String),
}

impl From<rocksdb::Error> for KeystoreError {
    fn from(err: rocksdb::Error) -> Self {
        KeystoreError::StorageError(format!("Keyfile storage error: {}", err.to_string()))
    }
}

impl From<serde_json::Error> for KeystoreError {
    fn from(err: serde_json::Error) -> Self {
        KeystoreError::StorageError(err.to_string())
    }
}

impl From<SerializeError> for KeystoreError {
    fn from(err: SerializeError) -> Self {
        KeystoreError::StorageError(err.to_string())
    }
}

impl From<str::Utf8Error> for KeystoreError {
    fn from(err: str::Utf8Error) -> Self {
        KeystoreError::StorageError(err.to_string())
    }
}

impl From<io::Error> for KeystoreError {
    fn from(err: io::Error) -> Self {
        KeystoreError::StorageError(err.to_string())
    }
}

impl fmt::Display for KeystoreError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            KeystoreError::StorageError(ref str) => write!(f, "KeyFile storage error: {}", str),
            KeystoreError::NotFound(ref str) => write!(f, "Missing KeyFile for address: {}", str),
        }
    }
}

impl error::Error for KeystoreError {
    fn description(&self) -> &str {
        "KeyFile storage error"
    }

    fn cause(&self) -> Option<&error::Error> {
        match *self {
            _ => None,
        }
    }
}