error.rs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. use std::fmt;
  2. use crate::{client, state};
  3. pub type Result<T> = std::result::Result<T, Error>;
  4. #[derive(Debug, Clone)]
  5. pub enum Error {
  6. Io(std::io::ErrorKind),
  7. /// VarInt was encoded in a non-minimal way
  8. PathNotFound,
  9. NonMinimalVarInt,
  10. /// Parsing And Encode/Decode errors
  11. ParseFailed(&'static str),
  12. ParseIntError,
  13. ParseBigIntError,
  14. ParseFloatError,
  15. FromHexError,
  16. UrlParseError,
  17. MalformedPacket,
  18. AddrParseError,
  19. Base58EncodeError(String),
  20. Base58DecodeError(String),
  21. Utf8Error,
  22. StrUtf8Error(String),
  23. TryIntoError,
  24. TryFromError,
  25. TryFromBigIntError,
  26. SerdeJsonError(String),
  27. TomlDeserializeError(String),
  28. TomlSerializeError(String),
  29. /// Contract
  30. BadVariableRefType,
  31. BadOperationType,
  32. BadConstraintType,
  33. InvalidParamName,
  34. InvalidParamType,
  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::InvalidParamType => f.write_str("Invalid param type"),
  104. Error::MissingParams => f.write_str("Missing params"),
  105. Error::VmError => f.write_str("VM error"),
  106. Error::BadContract => f.write_str("Contract is poorly defined"),
  107. Error::Groth16Error => f.write_str("Groth16 error"),
  108. Error::PlonkError => f.write_str("Plonk error"),
  109. Error::RusqliteError(ref err) => write!(f, "Rusqlite error {}", err),
  110. Error::OperationFailed => f.write_str("Operation failed"),
  111. Error::ConnectFailed => f.write_str("Connection failed"),
  112. Error::ConnectTimeout => f.write_str("Connection timed out"),
  113. Error::ChannelStopped => f.write_str("Channel stopped"),
  114. Error::ChannelTimeout => f.write_str("Channel timed out"),
  115. Error::ServiceStopped => f.write_str("Service stopped"),
  116. Error::Utf8Error => f.write_str("Malformed UTF8"),
  117. Error::StrUtf8Error(ref err) => write!(f, "Malformed UTF8: {}", err),
  118. Error::NoteDecryptionFailed => f.write_str("Unable to decrypt mint note"),
  119. Error::ServicesError(ref err) => write!(f, "Services error: {}", err),
  120. Error::ZmqError(ref err) => write!(f, "ZmqError: {}", err),
  121. Error::VerifyFailed => f.write_str("Verify failed"),
  122. Error::ClientFailed(ref err) => write!(f, "Client failed: {}", err),
  123. #[cfg(feature = "btc")]
  124. Error::BtcFailed(ref err) => write!(f, "Btc client failed: {}", err),
  125. #[cfg(feature = "sol")]
  126. Error::SolFailed(ref err) => write!(f, "Sol client failed: {}", err),
  127. #[cfg(feature = "eth")]
  128. Error::EthFailed(ref err) => write!(f, "Eth client failed: {}", err),
  129. Error::TryIntoError => f.write_str("TryInto error"),
  130. Error::TryFromError => f.write_str("TryFrom error"),
  131. Error::TryFromBigIntError => f.write_str("TryFromBigInt error"),
  132. Error::RocksdbError(ref err) => write!(f, "Rocksdb Error: {}", err),
  133. Error::SlabsStore(ref err) => write!(f, "SlabsStore Error: {}", err),
  134. Error::JsonRpcError(ref err) => write!(f, "JsonRpc Error: {}", err),
  135. Error::TreeFull => f.write_str("MerkleTree is full"),
  136. Error::NotSupportedNetwork => f.write_str("Not supported network"),
  137. Error::NotSupportedToken => f.write_str("Not supported token"),
  138. Error::BridgeError(ref err) => write!(f, "Bridge error: {}", err),
  139. Error::SerdeJsonError(ref err) => write!(f, "Json serialization error: {}", err),
  140. Error::TomlDeserializeError(ref err) => write!(f, "Toml parsing error: {}", err),
  141. Error::TomlSerializeError(ref err) => write!(f, "Toml parsing error: {}", err),
  142. Error::Base58EncodeError(ref err) => write!(f, "bs58 encode error: {}", err),
  143. Error::Base58DecodeError(ref err) => write!(f, "bs58 decode error: {}", err),
  144. Error::ConfigNotFound => f.write_str("No config file detected. Please create one."),
  145. Error::KeypairPathNotFound => f.write_str("No keypair file detected."),
  146. Error::CashierKeysNotFound => f.write_str("No cashier public keys detected."),
  147. Error::SetLoggerError => f.write_str("SetLoggerError"),
  148. Error::TokenParseError => f.write_str("Could not parse token parameter"),
  149. Error::TungsteniteError => f.write_str("TungsteniteError"),
  150. Error::NetworkParseError => f.write_str("Cannot parse network parameter"),
  151. }
  152. }
  153. }
  154. impl From<zeromq::ZmqError> for Error {
  155. fn from(err: zeromq::ZmqError) -> Error {
  156. Error::ZmqError(err.to_string())
  157. }
  158. }
  159. impl From<rocksdb::Error> for Error {
  160. fn from(err: rocksdb::Error) -> Error {
  161. Error::RocksdbError(err.to_string())
  162. }
  163. }
  164. impl From<serde_json::Error> for Error {
  165. fn from(err: serde_json::Error) -> Error {
  166. Error::SerdeJsonError(err.to_string())
  167. }
  168. }
  169. impl From<std::io::Error> for Error {
  170. fn from(err: std::io::Error) -> Error {
  171. Error::Io(err.kind())
  172. }
  173. }
  174. impl From<rusqlite::Error> for Error {
  175. fn from(err: rusqlite::Error) -> Error {
  176. Error::RusqliteError(err.to_string())
  177. }
  178. }
  179. impl<T> From<async_channel::SendError<T>> for Error {
  180. fn from(_err: async_channel::SendError<T>) -> Error {
  181. Error::AsyncChannelSenderError
  182. }
  183. }
  184. impl From<async_channel::RecvError> for Error {
  185. fn from(_err: async_channel::RecvError) -> Error {
  186. Error::AsyncChannelReceiverError
  187. }
  188. }
  189. impl From<async_native_tls::Error> for Error {
  190. fn from(_err: async_native_tls::Error) -> Error {
  191. Error::AsyncNativeTlsError
  192. }
  193. }
  194. impl From<std::net::AddrParseError> for Error {
  195. fn from(_err: std::net::AddrParseError) -> Error {
  196. Error::AddrParseError
  197. }
  198. }
  199. impl From<url::ParseError> for Error {
  200. fn from(_err: url::ParseError) -> Error {
  201. Error::UrlParseError
  202. }
  203. }
  204. impl From<hex::FromHexError> for Error {
  205. fn from(_err: hex::FromHexError) -> Error {
  206. Error::FromHexError
  207. }
  208. }
  209. impl From<std::num::ParseIntError> for Error {
  210. fn from(_err: std::num::ParseIntError) -> Error {
  211. Error::ParseIntError
  212. }
  213. }
  214. impl From<num_bigint::ParseBigIntError> for Error {
  215. fn from(_err: num_bigint::ParseBigIntError) -> Error {
  216. Error::ParseBigIntError
  217. }
  218. }
  219. impl From<num_bigint::TryFromBigIntError<num_bigint::BigUint>> for Error {
  220. fn from(_err: num_bigint::TryFromBigIntError<num_bigint::BigUint>) -> Error {
  221. Error::TryFromBigIntError
  222. }
  223. }
  224. impl From<std::num::ParseFloatError> for Error {
  225. fn from(_err: std::num::ParseFloatError) -> Error {
  226. Error::ParseFloatError
  227. }
  228. }
  229. impl From<std::string::FromUtf8Error> for Error {
  230. fn from(_err: std::string::FromUtf8Error) -> Error {
  231. Error::Utf8Error
  232. }
  233. }
  234. impl From<std::str::Utf8Error> for Error {
  235. fn from(err: std::str::Utf8Error) -> Error {
  236. Error::StrUtf8Error(err.to_string())
  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. #[cfg(feature = "btc")]
  250. impl From<crate::service::BtcFailed> for Error {
  251. fn from(err: crate::service::BtcFailed) -> Error {
  252. Error::BtcFailed(err.to_string())
  253. }
  254. }
  255. #[cfg(feature = "sol")]
  256. impl From<crate::service::SolFailed> for Error {
  257. fn from(err: crate::service::SolFailed) -> Error {
  258. Error::SolFailed(err.to_string())
  259. }
  260. }
  261. #[cfg(feature = "eth")]
  262. impl From<crate::service::EthFailed> for Error {
  263. fn from(err: crate::service::EthFailed) -> Error {
  264. Error::EthFailed(err.to_string())
  265. }
  266. }
  267. impl From<toml::de::Error> for Error {
  268. fn from(err: toml::de::Error) -> Error {
  269. Error::TomlDeserializeError(err.to_string())
  270. }
  271. }
  272. impl From<toml::ser::Error> for Error {
  273. fn from(err: toml::ser::Error) -> Error {
  274. Error::TomlSerializeError(err.to_string())
  275. }
  276. }
  277. impl From<bs58::encode::Error> for Error {
  278. fn from(err: bs58::encode::Error) -> Error {
  279. Error::Base58EncodeError(err.to_string())
  280. }
  281. }
  282. impl From<bs58::decode::Error> for Error {
  283. fn from(err: bs58::decode::Error) -> Error {
  284. Error::Base58DecodeError(err.to_string())
  285. }
  286. }
  287. impl From<log::SetLoggerError> for Error {
  288. fn from(_err: log::SetLoggerError) -> Error {
  289. Error::SetLoggerError
  290. }
  291. }
  292. impl From<tungstenite::Error> for Error {
  293. fn from(_err: tungstenite::Error) -> Error {
  294. Error::TungsteniteError
  295. }
  296. }
  297. impl From<halo2::plonk::Error> for Error {
  298. fn from(_err: halo2::plonk::Error) -> Error {
  299. Error::PlonkError
  300. }
  301. }