1use std::fmt;
4
5pub type Result<T> = std::result::Result<T, Error>;
7
8#[derive(Clone, Debug, Eq, PartialEq)]
10pub enum Error {
11 AddressMalformed {
13 addr: String,
15 },
16
17 DigestMalformed,
19
20 SigningKeyNotFound {
22 addr: String,
24 },
25
26 SigningFailed {
28 reason: String,
30 },
31}
32
33impl Error {
34 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 Error::AddressMalformed {
82 addr: "0xdeadbeef".to_owned(),
83 }
84 }
85}