cfxstore/
error.rs

1// Copyright 2015-2019 Parity Technologies (UK) Ltd.
2// This file is part of Parity Ethereum.
3
4// Parity Ethereum is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Parity Ethereum is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Parity Ethereum.  If not, see <http://www.gnu.org/licenses/>.
16
17use cfxkey::{self, DerivationError, Error as EthKeyError};
18use std::{fmt, io::Error as IoError};
19
20/// Account-related errors.
21#[derive(Debug)]
22pub enum Error {
23    /// IO error
24    Io(IoError),
25    /// Invalid Password
26    InvalidPassword,
27    /// Account's secret is invalid.
28    InvalidSecret,
29    /// Invalid Vault Crypto meta.
30    InvalidCryptoMeta,
31    /// Invalid Account.
32    InvalidAccount,
33    /// Invalid Message.
34    InvalidMessage,
35    /// Invalid Key File
36    InvalidKeyFile(String),
37    /// Vaults are not supported.
38    VaultsAreNotSupported,
39    /// Unsupported vault
40    UnsupportedVault,
41    /// Invalid vault name
42    InvalidVaultName,
43    /// Vault not found
44    VaultNotFound,
45    /// Account creation failed.
46    CreationFailed,
47    /// `EthKey` error
48    EthKey(EthKeyError),
49    /// `cfx_crypto::crypto::Error`
50    EthKeyCrypto(cfx_crypto::crypto::Error),
51    /// `EthCrypto` error
52    EthCrypto(String),
53    /// Derivation error
54    Derivation(DerivationError),
55    /// Custom error
56    Custom(String),
57}
58
59impl fmt::Display for Error {
60    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
61        let s = match *self {
62            Error::Io(ref err) => err.to_string(),
63            Error::InvalidPassword => "Invalid password".into(),
64            Error::InvalidSecret => "Invalid secret".into(),
65            Error::InvalidCryptoMeta => "Invalid crypted metadata".into(),
66            Error::InvalidAccount => "Invalid account".into(),
67            Error::InvalidMessage => "Invalid message".into(),
68            Error::InvalidKeyFile(ref reason) => {
69                format!("Invalid key file: {}", reason)
70            }
71            Error::VaultsAreNotSupported => "Vaults are not supported".into(),
72            Error::UnsupportedVault => {
73                "Vault is not supported for this operation".into()
74            }
75            Error::InvalidVaultName => "Invalid vault name".into(),
76            Error::VaultNotFound => "Vault not found".into(),
77            Error::CreationFailed => "Account creation failed".into(),
78            Error::EthKey(ref err) => err.to_string(),
79            Error::EthKeyCrypto(ref err) => err.to_string(),
80            Error::EthCrypto(ref err) => {
81                format!("eth crypto: {}", err)
82            }
83            Error::Derivation(ref err) => {
84                format!("Derivation error: {:?}", err)
85            }
86            Error::Custom(ref s) => s.clone(),
87        };
88
89        write!(f, "{}", s)
90    }
91}
92
93impl From<IoError> for Error {
94    fn from(err: IoError) -> Self { Error::Io(err) }
95}
96
97impl From<EthKeyError> for Error {
98    fn from(err: EthKeyError) -> Self { Error::EthKey(err) }
99}
100
101impl From<cfx_crypto::crypto::Error> for Error {
102    fn from(err: cfx_crypto::crypto::Error) -> Self { Error::EthKeyCrypto(err) }
103}
104
105impl From<DerivationError> for Error {
106    fn from(err: DerivationError) -> Self { Error::Derivation(err) }
107}