iqkms_ethereum/
error.rs

1//! Error types.
2
3use std::fmt;
4
5/// `Result` type with the `iqkms-ethereum` crate's [`Error`] type.
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Error type.
9#[derive(Clone, Debug, Eq, PartialEq)]
10pub enum Error {
11    /// Malformed Ethereum address.
12    AddressMalformed {
13        /// Requested address.
14        addr: String,
15    },
16
17    /// Malformed Keccak256 digest.
18    DigestMalformed,
19
20    /// Signing key not found.
21    SigningKeyNotFound {
22        /// Requested address.
23        addr: String,
24    },
25
26    /// Signing operation failed.
27    SigningFailed {
28        /// Reason why the signing operation failed.
29        reason: String,
30    },
31}
32
33impl Error {
34    /// Get the `tonic::Code` associated with this error.
35    fn code(&self) -> tonic::Code {
36        match self {
37            Error::AddressMalformed { .. } => tonic::Code::InvalidArgument,
38            Error::DigestMalformed { .. } => tonic::Code::InvalidArgument,
39            Error::SigningKeyNotFound { .. } => tonic::Code::NotFound,
40            Error::SigningFailed { .. } => tonic::Code::Internal,
41        }
42    }
43}
44
45impl fmt::Display for Error {
46    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47        match self {
48            Error::AddressMalformed { addr } => {
49                write!(f, "Ethereum address malformed: \"{}\"", addr)
50            }
51            Error::DigestMalformed => write!(f, "Keccak256 digest malformed"),
52            Error::SigningKeyNotFound { addr } => write!(f, "signing key not found: \"{}\"", addr),
53            Error::SigningFailed { reason } => f.write_str(reason),
54        }
55    }
56}
57
58impl From<Error> for tonic::Status {
59    fn from(error: Error) -> tonic::Status {
60        tonic::Status::new(error.code(), error.to_string())
61    }
62}
63
64impl From<signing::Error> for Error {
65    fn from(_: signing::Error) -> Error {
66        Error::SigningFailed {
67            reason: "signing operation failed".to_owned(),
68        }
69    }
70}
71
72impl From<signing::signature::Error> for Error {
73    fn from(_: signing::signature::Error) -> Error {
74        signing::Error.into()
75    }
76}
77
78impl From<types::Error> for Error {
79    fn from(_: types::Error) -> Error {
80        // TODO(tarcieri): non-bogus implementation
81        Error::AddressMalformed {
82            addr: "0xdeadbeef".to_owned(),
83        }
84    }
85}