error.rs 11 KB

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