error.rs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. use std::fmt;
  2. use crate::client;
  3. use crate::state;
  4. use crate::vm::ZkVmError;
  5. pub type Result<T> = std::result::Result<T, Error>;
  6. #[derive(Debug, Clone)]
  7. pub enum Error {
  8. Io(std::io::ErrorKind),
  9. /// VarInt was encoded in a non-minimal way
  10. PathNotFound,
  11. NonMinimalVarInt,
  12. /// Parsing error
  13. ParseFailed(&'static str),
  14. ParseIntError,
  15. ParseFloatError,
  16. UrlParseError,
  17. AsyncChannelSenderError,
  18. AsyncChannelReceiverError,
  19. AsyncNativeTlsError,
  20. MalformedPacket,
  21. AddrParseError,
  22. BadVariableRefType,
  23. BadOperationType,
  24. BadConstraintType,
  25. InvalidParamName,
  26. MissingParams,
  27. VmError,
  28. BadContract,
  29. Groth16Error,
  30. RusqliteError(String),
  31. OperationFailed,
  32. ConnectFailed,
  33. ConnectTimeout,
  34. ChannelStopped,
  35. ChannelTimeout,
  36. ServiceStopped,
  37. Utf8Error,
  38. StrUtf8Error(String),
  39. NoteDecryptionFailed,
  40. ServicesError(&'static str),
  41. ZmqError(String),
  42. VerifyFailed,
  43. ClientFailed(String),
  44. #[cfg(feature = "btc")]
  45. BtcFailed(String),
  46. #[cfg(feature = "sol")]
  47. SolFailed(String),
  48. TryIntoError,
  49. TryFromError,
  50. JsonRpcError(String),
  51. RocksdbError(String),
  52. TreeFull,
  53. BridgeError(String),
  54. NotSupportedNetwork,
  55. SerdeJsonError(String),
  56. TomlDeserializeError(String),
  57. TomlSerializeError(String),
  58. CashierNoReply,
  59. CashierInvalidTokenId(String),
  60. Base58EncodeError(String),
  61. Base58DecodeError(String),
  62. ConfigNotFound,
  63. SetLoggerError,
  64. TokenParseError,
  65. TungsteniteError,
  66. }
  67. impl std::error::Error for Error {}
  68. impl fmt::Display for Error {
  69. fn fmt(&self, f: &mut fmt::Formatter) -> std::fmt::Result {
  70. match *self {
  71. Error::PathNotFound => f.write_str("Cannot find home directory"),
  72. Error::Io(ref err) => write!(f, "io error:{:?}", err),
  73. Error::NonMinimalVarInt => f.write_str("non-minimal varint"),
  74. Error::ParseFailed(ref err) => write!(f, "parse failed: {}", err),
  75. Error::ParseIntError => f.write_str("Parse int error"),
  76. Error::ParseFloatError => f.write_str("Parse float error"),
  77. Error::UrlParseError => f.write_str("Failed to parse URL"),
  78. Error::AsyncChannelSenderError => f.write_str("Async_channel sender error"),
  79. Error::AsyncChannelReceiverError => f.write_str("Async_channel receiver error"),
  80. Error::AsyncNativeTlsError => f.write_str("Async_Native_TLS error"),
  81. Error::MalformedPacket => f.write_str("Malformed packet"),
  82. Error::AddrParseError => f.write_str("Unable to parse address"),
  83. Error::BadVariableRefType => f.write_str("Bad variable ref type byte"),
  84. Error::BadOperationType => f.write_str("Bad operation type byte"),
  85. Error::BadConstraintType => f.write_str("Bad constraint type byte"),
  86. Error::InvalidParamName => f.write_str("Invalid param name"),
  87. Error::MissingParams => f.write_str("Missing params"),
  88. Error::VmError => f.write_str("VM error"),
  89. Error::BadContract => f.write_str("Contract is poorly defined"),
  90. Error::Groth16Error => f.write_str("Groth16 error"),
  91. Error::RusqliteError(ref err) => write!(f, "Rusqlite error {}", err),
  92. Error::OperationFailed => f.write_str("Operation failed"),
  93. Error::ConnectFailed => f.write_str("Connection failed"),
  94. Error::ConnectTimeout => f.write_str("Connection timed out"),
  95. Error::ChannelStopped => f.write_str("Channel stopped"),
  96. Error::ChannelTimeout => f.write_str("Channel timed out"),
  97. Error::ServiceStopped => f.write_str("Service stopped"),
  98. Error::Utf8Error => f.write_str("Malformed UTF8"),
  99. Error::StrUtf8Error(ref err) => write!(f, "Malformed UTF8: {}", err),
  100. Error::NoteDecryptionFailed => f.write_str("Unable to decrypt mint note"),
  101. Error::ServicesError(ref err) => write!(f, "Services error: {}", err),
  102. Error::ZmqError(ref err) => write!(f, "ZmqError: {}", err),
  103. Error::VerifyFailed => f.write_str("Verify failed"),
  104. Error::ClientFailed(ref err) => write!(f, "Client failed: {}", err),
  105. #[cfg(feature = "btc")]
  106. Error::BtcFailed(ref err) => write!(f, "Btc client failed: {}", err),
  107. #[cfg(feature = "sol")]
  108. Error::SolFailed(ref err) => write!(f, "Sol client failed: {}", err),
  109. Error::TryIntoError => f.write_str("TryInto error"),
  110. Error::TryFromError => f.write_str("TryFrom error"),
  111. Error::RocksdbError(ref err) => write!(f, "Rocksdb Error: {}", err),
  112. Error::JsonRpcError(ref err) => write!(f, "JsonRpc Error: {}", err),
  113. Error::TreeFull => f.write_str("MerkleTree is full"),
  114. Error::NotSupportedNetwork => {
  115. f.write_str("Not supported network inside cashierd config file")
  116. }
  117. Error::BridgeError(ref err) => write!(f, "Bridge error: {}", err),
  118. Error::SerdeJsonError(ref err) => write!(f, "Json serialization error: {}", err),
  119. Error::TomlDeserializeError(ref err) => write!(f, "Toml parsing error: {}", err),
  120. Error::TomlSerializeError(ref err) => write!(f, "Toml parsing error: {}", err),
  121. Error::Base58EncodeError(ref err) => write!(f, "bs58 encode error: {}", err),
  122. Error::Base58DecodeError(ref err) => write!(f, "bs58 decode error: {}", err),
  123. Error::CashierInvalidTokenId(ref err) => write!(f, "Cashier invalid token id: {}", err),
  124. Error::CashierNoReply => f.write_str("Cashier did not reply with token address"),
  125. Error::ConfigNotFound => {
  126. f.write_str("No config file detected. Please create a config file")
  127. }
  128. Error::SetLoggerError => f.write_str("SetLoggerError"),
  129. Error::TokenParseError => f.write_str("Could not parse token list"),
  130. Error::TungsteniteError => f.write_str("TungsteniteError"),
  131. }
  132. }
  133. }
  134. impl From<zeromq::ZmqError> for Error {
  135. fn from(err: zeromq::ZmqError) -> Error {
  136. Error::ZmqError(err.to_string())
  137. }
  138. }
  139. impl From<rocksdb::Error> for Error {
  140. fn from(err: rocksdb::Error) -> Error {
  141. Error::RocksdbError(err.to_string())
  142. }
  143. }
  144. impl From<serde_json::Error> for Error {
  145. fn from(err: serde_json::Error) -> Error {
  146. Error::SerdeJsonError(err.to_string())
  147. }
  148. }
  149. impl From<std::io::Error> for Error {
  150. fn from(err: std::io::Error) -> Error {
  151. Error::Io(err.kind())
  152. }
  153. }
  154. impl From<rusqlite::Error> for Error {
  155. fn from(err: rusqlite::Error) -> Error {
  156. Error::RusqliteError(err.to_string())
  157. }
  158. }
  159. impl From<ZkVmError> for Error {
  160. fn from(_err: ZkVmError) -> Error {
  161. Error::VmError
  162. }
  163. }
  164. impl From<bellman::SynthesisError> for Error {
  165. fn from(_err: bellman::SynthesisError) -> Error {
  166. Error::Groth16Error
  167. }
  168. }
  169. impl<T> From<async_channel::SendError<T>> for Error {
  170. fn from(_err: async_channel::SendError<T>) -> Error {
  171. Error::AsyncChannelSenderError
  172. }
  173. }
  174. impl From<async_channel::RecvError> for Error {
  175. fn from(_err: async_channel::RecvError) -> Error {
  176. Error::AsyncChannelReceiverError
  177. }
  178. }
  179. impl From<async_native_tls::Error> for Error {
  180. fn from(_err: async_native_tls::Error) -> Error {
  181. Error::AsyncNativeTlsError
  182. }
  183. }
  184. impl From<std::net::AddrParseError> for Error {
  185. fn from(_err: std::net::AddrParseError) -> Error {
  186. Error::AddrParseError
  187. }
  188. }
  189. impl From<url::ParseError> for Error {
  190. fn from(_err: url::ParseError) -> Error {
  191. Error::UrlParseError
  192. }
  193. }
  194. impl From<std::num::ParseIntError> for Error {
  195. fn from(_err: std::num::ParseIntError) -> Error {
  196. Error::ParseIntError
  197. }
  198. }
  199. impl From<std::num::ParseFloatError> for Error {
  200. fn from(_err: std::num::ParseFloatError) -> Error {
  201. Error::ParseFloatError
  202. }
  203. }
  204. impl From<std::string::FromUtf8Error> for Error {
  205. fn from(_err: std::string::FromUtf8Error) -> Error {
  206. Error::Utf8Error
  207. }
  208. }
  209. impl From<std::str::Utf8Error> for Error {
  210. fn from(err: std::str::Utf8Error) -> Error {
  211. Error::StrUtf8Error(err.to_string())
  212. }
  213. }
  214. impl From<state::VerifyFailed> for Error {
  215. fn from(_err: state::VerifyFailed) -> Error {
  216. Error::VerifyFailed
  217. }
  218. }
  219. impl From<client::ClientFailed> for Error {
  220. fn from(err: client::ClientFailed) -> Error {
  221. Error::ClientFailed(err.to_string())
  222. }
  223. }
  224. #[cfg(feature = "btc")]
  225. impl From<crate::service::BtcFailed> for Error {
  226. fn from(err: crate::service::BtcFailed) -> Error {
  227. Error::BtcFailed(err.to_string())
  228. }
  229. }
  230. #[cfg(feature = "sol")]
  231. impl From<crate::service::SolFailed> for Error {
  232. fn from(err: crate::service::SolFailed) -> Error {
  233. Error::SolFailed(err.to_string())
  234. }
  235. }
  236. impl From<toml::de::Error> for Error {
  237. fn from(err: toml::de::Error) -> Error {
  238. Error::TomlDeserializeError(err.to_string())
  239. }
  240. }
  241. impl From<toml::ser::Error> for Error {
  242. fn from(err: toml::ser::Error) -> Error {
  243. Error::TomlSerializeError(err.to_string())
  244. }
  245. }
  246. impl From<bs58::encode::Error> for Error {
  247. fn from(err: bs58::encode::Error) -> Error {
  248. Error::Base58EncodeError(err.to_string())
  249. }
  250. }
  251. impl From<bs58::decode::Error> for Error {
  252. fn from(err: bs58::decode::Error) -> Error {
  253. Error::Base58DecodeError(err.to_string())
  254. }
  255. }
  256. impl From<log::SetLoggerError> for Error {
  257. fn from(_err: log::SetLoggerError) -> Error {
  258. Error::SetLoggerError
  259. }
  260. }
  261. impl From<tungstenite::Error> for Error {
  262. fn from(_err: tungstenite::Error) -> Error {
  263. Error::TungsteniteError
  264. }
  265. }