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. MissingParams,
  36. VmError,
  37. BadContract,
  38. Groth16Error,
  39. PlonkError,
  40. OperationFailed,
  41. NoteDecryptionFailed,
  42. VerifyFailed,
  43. TreeFull,
  44. /// Service
  45. ServicesError(&'static str),
  46. ClientFailed(String),
  47. #[cfg(feature = "btc")]
  48. BtcFailed(String),
  49. #[cfg(feature = "sol")]
  50. SolFailed(String),
  51. #[cfg(feature = "eth")]
  52. EthFailed(String),
  53. BridgeError(String),
  54. ZmqError(String),
  55. /// Database/Sql errors
  56. RocksdbError(String),
  57. RusqliteError(String),
  58. SlabsStore(String),
  59. /// RPC errors
  60. JsonRpcError(String),
  61. NotSupportedNetwork,
  62. NotSupportedToken,
  63. TokenParseError,
  64. NetworkParseError,
  65. AsyncNativeTlsError,
  66. TungsteniteError,
  67. /// Network
  68. ConnectFailed,
  69. ConnectTimeout,
  70. ChannelStopped,
  71. ChannelTimeout,
  72. ServiceStopped,
  73. /// Util
  74. ConfigNotFound,
  75. KeypairPathNotFound,
  76. CashierKeysNotFound,
  77. SetLoggerError,
  78. AsyncChannelSenderError,
  79. AsyncChannelReceiverError,
  80. }
  81. impl std::error::Error for Error {}
  82. impl fmt::Display for Error {
  83. fn fmt(&self, f: &mut fmt::Formatter) -> std::fmt::Result {
  84. match *self {
  85. Error::PathNotFound => f.write_str("Cannot find home directory"),
  86. Error::Io(ref err) => write!(f, "io error:{:?}", err),
  87. Error::NonMinimalVarInt => f.write_str("non-minimal varint"),
  88. Error::ParseFailed(ref err) => write!(f, "parse failed: {}", err),
  89. Error::ParseIntError => f.write_str("Parse int error"),
  90. Error::ParseBigIntError => f.write_str("Parse big int error"),
  91. Error::ParseFloatError => f.write_str("Parse float error"),
  92. Error::UrlParseError => f.write_str("Failed to parse URL"),
  93. Error::FromHexError => f.write_str("Failed to convert from hex"),
  94. Error::AsyncChannelSenderError => f.write_str("Async_channel sender error"),
  95. Error::AsyncChannelReceiverError => f.write_str("Async_channel receiver error"),
  96. Error::AsyncNativeTlsError => f.write_str("Async_Native_TLS error"),
  97. Error::MalformedPacket => f.write_str("Malformed packet"),
  98. Error::AddrParseError => f.write_str("Unable to parse address"),
  99. Error::BadVariableRefType => f.write_str("Bad variable ref type byte"),
  100. Error::BadOperationType => f.write_str("Bad operation type byte"),
  101. Error::BadConstraintType => f.write_str("Bad constraint type byte"),
  102. Error::InvalidParamName => f.write_str("Invalid param name"),
  103. Error::MissingParams => f.write_str("Missing params"),
  104. Error::VmError => f.write_str("VM error"),
  105. Error::BadContract => f.write_str("Contract is poorly defined"),
  106. Error::Groth16Error => f.write_str("Groth16 error"),
  107. Error::PlonkError => f.write_str("Plonk error"),
  108. Error::RusqliteError(ref err) => write!(f, "Rusqlite error {}", err),
  109. Error::OperationFailed => f.write_str("Operation failed"),
  110. Error::ConnectFailed => f.write_str("Connection failed"),
  111. Error::ConnectTimeout => f.write_str("Connection timed out"),
  112. Error::ChannelStopped => f.write_str("Channel stopped"),
  113. Error::ChannelTimeout => f.write_str("Channel timed out"),
  114. Error::ServiceStopped => f.write_str("Service stopped"),
  115. Error::Utf8Error => f.write_str("Malformed UTF8"),
  116. Error::StrUtf8Error(ref err) => write!(f, "Malformed UTF8: {}", err),
  117. Error::NoteDecryptionFailed => f.write_str("Unable to decrypt mint note"),
  118. Error::ServicesError(ref err) => write!(f, "Services error: {}", err),
  119. Error::ZmqError(ref err) => write!(f, "ZmqError: {}", err),
  120. Error::VerifyFailed => f.write_str("Verify failed"),
  121. Error::ClientFailed(ref err) => write!(f, "Client failed: {}", err),
  122. #[cfg(feature = "btc")]
  123. Error::BtcFailed(ref err) => write!(f, "Btc client failed: {}", err),
  124. #[cfg(feature = "sol")]
  125. Error::SolFailed(ref err) => write!(f, "Sol client failed: {}", err),
  126. #[cfg(feature = "eth")]
  127. Error::EthFailed(ref err) => write!(f, "Eth client failed: {}", err),
  128. Error::TryIntoError => f.write_str("TryInto error"),
  129. Error::TryFromError => f.write_str("TryFrom error"),
  130. Error::TryFromBigIntError => f.write_str("TryFromBigInt error"),
  131. Error::RocksdbError(ref err) => write!(f, "Rocksdb Error: {}", err),
  132. Error::SlabsStore(ref err) => write!(f, "SlabsStore Error: {}", err),
  133. Error::JsonRpcError(ref err) => write!(f, "JsonRpc Error: {}", err),
  134. Error::TreeFull => f.write_str("MerkleTree is full"),
  135. Error::NotSupportedNetwork => f.write_str("Not supported network"),
  136. Error::NotSupportedToken => f.write_str("Not supported token"),
  137. Error::BridgeError(ref err) => write!(f, "Bridge error: {}", err),
  138. Error::SerdeJsonError(ref err) => write!(f, "Json serialization error: {}", err),
  139. Error::TomlDeserializeError(ref err) => write!(f, "Toml parsing error: {}", err),
  140. Error::TomlSerializeError(ref err) => write!(f, "Toml parsing error: {}", err),
  141. Error::Base58EncodeError(ref err) => write!(f, "bs58 encode error: {}", err),
  142. Error::Base58DecodeError(ref err) => write!(f, "bs58 decode error: {}", err),
  143. Error::ConfigNotFound => f.write_str("No config file detected. Please create one."),
  144. Error::KeypairPathNotFound => f.write_str("No keypair file detected."),
  145. Error::CashierKeysNotFound => f.write_str("No cashier public keys detected."),
  146. Error::SetLoggerError => f.write_str("SetLoggerError"),
  147. Error::TokenParseError => f.write_str("Could not parse token parameter"),
  148. Error::TungsteniteError => f.write_str("TungsteniteError"),
  149. Error::NetworkParseError => f.write_str("Cannot parse network parameter"),
  150. }
  151. }
  152. }
  153. impl From<zeromq::ZmqError> for Error {
  154. fn from(err: zeromq::ZmqError) -> Error {
  155. Error::ZmqError(err.to_string())
  156. }
  157. }
  158. impl From<rocksdb::Error> for Error {
  159. fn from(err: rocksdb::Error) -> Error {
  160. Error::RocksdbError(err.to_string())
  161. }
  162. }
  163. impl From<serde_json::Error> for Error {
  164. fn from(err: serde_json::Error) -> Error {
  165. Error::SerdeJsonError(err.to_string())
  166. }
  167. }
  168. impl From<std::io::Error> for Error {
  169. fn from(err: std::io::Error) -> Error {
  170. Error::Io(err.kind())
  171. }
  172. }
  173. impl From<rusqlite::Error> for Error {
  174. fn from(err: rusqlite::Error) -> Error {
  175. Error::RusqliteError(err.to_string())
  176. }
  177. }
  178. impl<T> From<async_channel::SendError<T>> for Error {
  179. fn from(_err: async_channel::SendError<T>) -> Error {
  180. Error::AsyncChannelSenderError
  181. }
  182. }
  183. impl From<async_channel::RecvError> for Error {
  184. fn from(_err: async_channel::RecvError) -> Error {
  185. Error::AsyncChannelReceiverError
  186. }
  187. }
  188. impl From<async_native_tls::Error> for Error {
  189. fn from(_err: async_native_tls::Error) -> Error {
  190. Error::AsyncNativeTlsError
  191. }
  192. }
  193. impl From<std::net::AddrParseError> for Error {
  194. fn from(_err: std::net::AddrParseError) -> Error {
  195. Error::AddrParseError
  196. }
  197. }
  198. impl From<url::ParseError> for Error {
  199. fn from(_err: url::ParseError) -> Error {
  200. Error::UrlParseError
  201. }
  202. }
  203. impl From<hex::FromHexError> for Error {
  204. fn from(_err: hex::FromHexError) -> Error {
  205. Error::FromHexError
  206. }
  207. }
  208. impl From<std::num::ParseIntError> for Error {
  209. fn from(_err: std::num::ParseIntError) -> Error {
  210. Error::ParseIntError
  211. }
  212. }
  213. impl From<num_bigint::ParseBigIntError> for Error {
  214. fn from(_err: num_bigint::ParseBigIntError) -> Error {
  215. Error::ParseBigIntError
  216. }
  217. }
  218. impl From<num_bigint::TryFromBigIntError<num_bigint::BigUint>> for Error {
  219. fn from(_err: num_bigint::TryFromBigIntError<num_bigint::BigUint>) -> Error {
  220. Error::TryFromBigIntError
  221. }
  222. }
  223. impl From<std::num::ParseFloatError> for Error {
  224. fn from(_err: std::num::ParseFloatError) -> Error {
  225. Error::ParseFloatError
  226. }
  227. }
  228. impl From<std::string::FromUtf8Error> for Error {
  229. fn from(_err: std::string::FromUtf8Error) -> Error {
  230. Error::Utf8Error
  231. }
  232. }
  233. impl From<std::str::Utf8Error> for Error {
  234. fn from(err: std::str::Utf8Error) -> Error {
  235. Error::StrUtf8Error(err.to_string())
  236. }
  237. }
  238. /*
  239. impl From<state::VerifyFailed> for Error {
  240. fn from(_err: state::VerifyFailed) -> Error {
  241. Error::VerifyFailed
  242. }
  243. }
  244. impl From<client::ClientFailed> for Error {
  245. fn from(err: client::ClientFailed) -> Error {
  246. Error::ClientFailed(err.to_string())
  247. }
  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. }