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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//! Error types

use crate::{chain, prost};
use std::{
    any::Any,
    fmt::{self, Display},
    io,
};
use tendermint::amino_types::validate::ValidationError;

/// Error type
#[derive(Debug)]
pub struct Error(abscissa::Error<ErrorKind>);

impl Error {
    /// Create an error from a panic
    pub fn from_panic(msg: &dyn Any) -> Self {
        if let Some(e) = msg.downcast_ref::<String>() {
            err!(ErrorKind::PanicError, e)
        } else if let Some(e) = msg.downcast_ref::<&str>() {
            err!(ErrorKind::PanicError, e)
        } else {
            err!(ErrorKind::PanicError, "unknown cause")
        }
        .into()
    }
}

/// Kinds of errors
#[derive(Copy, Clone, Eq, PartialEq, Debug, Fail)]
pub enum ErrorKind {
    /// Access denied
    #[fail(display = "access denied")]
    #[cfg(feature = "yubihsm")]
    AccessError,

    /// Error in configuration file
    #[fail(display = "config error")]
    ConfigError,

    /// KMS internal panic
    #[fail(display = "internal crash")]
    PanicError,

    /// Cryptographic operation failed
    #[fail(display = "cryptographic error")]
    CryptoError,

    /// Error running a subcommand to update chain state
    #[fail(display = "subcommand hook failed")]
    HookError,

    /// Malformatted or otherwise invalid cryptographic key
    #[fail(display = "invalid key")]
    InvalidKey,

    /// Validation of consensus message failed
    #[fail(display = "invalid consensus message")]
    InvalidMessageError,

    /// Input/output error
    #[fail(display = "I/O error")]
    IoError,

    /// Parse error
    #[fail(display = "parse error")]
    ParseError,

    /// Network protocol-related errors
    #[fail(display = "protocol error")]
    ProtocolError,

    /// Serialization error
    #[fail(display = "serialization error")]
    SerializationError,

    /// Signing operation failed
    #[fail(display = "signing operation failed")]
    SigningError,

    /// Verification operation failed
    #[fail(display = "verification failed")]
    VerificationError,

    /// Signature invalid
    #[fail(display = "attempted double sign")]
    DoubleSign,

    ///Request a Signature above max height
    #[fail(display = "requested signature above stop height")]
    ExceedMaxHeight,
}

impl Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}

impl From<ErrorKind> for Error {
    fn from(kind: ErrorKind) -> Error {
        Error(abscissa::Error::new(kind, None))
    }
}

impl From<abscissa::Error<ErrorKind>> for Error {
    fn from(other: abscissa::Error<ErrorKind>) -> Self {
        Error(other)
    }
}

impl From<io::Error> for Error {
    fn from(other: io::Error) -> Self {
        err!(ErrorKind::IoError, other).into()
    }
}

impl From<prost::DecodeError> for Error {
    fn from(other: prost::DecodeError) -> Self {
        err!(ErrorKind::ProtocolError, other).into()
    }
}

impl From<prost::EncodeError> for Error {
    fn from(other: prost::EncodeError) -> Self {
        err!(ErrorKind::ProtocolError, other).into()
    }
}

impl From<serde_json::error::Error> for Error {
    fn from(other: serde_json::error::Error) -> Self {
        err!(ErrorKind::SerializationError, other).into()
    }
}

impl From<tendermint::Error> for Error {
    fn from(other: tendermint::error::Error) -> Self {
        let kind = match other {
            tendermint::Error::Crypto => ErrorKind::CryptoError,
            tendermint::Error::InvalidKey => ErrorKind::InvalidKey,
            tendermint::Error::Io => ErrorKind::IoError,
            tendermint::Error::Protocol => ErrorKind::ProtocolError,
            tendermint::Error::Length
            | tendermint::Error::Parse
            | tendermint::Error::OutOfRange => ErrorKind::ParseError,
            tendermint::Error::SignatureInvalid => ErrorKind::VerificationError,
        };

        abscissa::Error::new(kind, None).into()
    }
}

impl From<ValidationError> for Error {
    fn from(other: ValidationError) -> Self {
        err!(ErrorKind::InvalidMessageError, other).into()
    }
}

impl From<chain::state::StateError> for Error {
    fn from(other: chain::state::StateError) -> Self {
        err!(ErrorKind::DoubleSign, other).into()
    }
}