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
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//! # JSON RPC module errors

use super::core;
use super::storage;
use contract;
use hdwallet;
use hex;
use jsonrpc_core;
use keystore;
use mnemonic;
use reqwest;
use serde_json;
use std::{error, fmt, io};

/// JSON RPC errors
#[derive(Debug)]
pub enum Error {
    /// Http client error
    HttpClient(reqwest::Error),
    /// RPC error
    RPC(jsonrpc_core::Error),
    /// Invalid data format
    InvalidDataFormat(String),
    /// Storage error
    StorageError(String),
    /// Storage error
    ContractAbiError(String),
    /// Mnemonic phrase operations error
    MnemonicError(String),
    /// Addressbook operations error
    AddressbookError(String),
}

impl From<storage::addressbook::error::AddressbookError> for Error {
    fn from(err: storage::addressbook::error::AddressbookError) -> Self {
        Error::AddressbookError(err.to_string())
    }
}

impl From<keystore::Error> for Error {
    fn from(err: keystore::Error) -> Self {
        Error::InvalidDataFormat(format!("keystore: {}", err.to_string()))
    }
}

impl From<io::Error> for Error {
    fn from(e: io::Error) -> Self {
        Error::InvalidDataFormat(e.to_string())
    }
}

impl From<reqwest::Error> for Error {
    fn from(err: reqwest::Error) -> Self {
        Error::HttpClient(err)
    }
}

impl From<core::Error> for Error {
    fn from(err: core::Error) -> Self {
        Error::InvalidDataFormat(err.to_string())
    }
}

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

impl From<hex::FromHexError> for Error {
    fn from(err: hex::FromHexError) -> Self {
        Error::InvalidDataFormat(err.to_string())
    }
}

impl From<jsonrpc_core::Error> for Error {
    fn from(err: jsonrpc_core::Error) -> Self {
        Error::RPC(err)
    }
}

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

impl From<contract::Error> for Error {
    fn from(err: contract::Error) -> Self {
        Error::ContractAbiError(err.to_string())
    }
}

impl From<mnemonic::Error> for Error {
    fn from(err: mnemonic::Error) -> Self {
        Error::MnemonicError(err.to_string())
    }
}

impl From<hdwallet::Error> for Error {
    fn from(err: hdwallet::Error) -> Self {
        Error::MnemonicError(err.to_string())
    }
}

impl Into<jsonrpc_core::Error> for Error {
    fn into(self) -> jsonrpc_core::Error {
        jsonrpc_core::Error::internal_error()
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Error::HttpClient(ref err) => write!(f, "HTTP client error: {}", err),
            Error::RPC(ref err) => write!(f, "RPC error: {:?}", err),
            Error::InvalidDataFormat(ref str) => write!(f, "Invalid data format: {}", str),
            Error::StorageError(ref str) => write!(f, "Keyfile storage error: {}", str),
            Error::ContractAbiError(ref str) => write!(f, "Contract ABI error: {}", str),
            Error::MnemonicError(ref str) => write!(f, "Mnemonic error: {}", str),
            Error::AddressbookError(ref str) => write!(f, "Addressbook error: {}", str),
        }
    }
}

impl error::Error for Error {
    fn description(&self) -> &str {
        "JSON RPC errors"
    }

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