error.rs 8.7 KB

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