error.rs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. use jsonrpc_core::*;
  2. use std::fmt;
  3. use crate::client;
  4. use crate::state;
  5. use crate::vm::ZkVmError;
  6. pub type Result<T> = std::result::Result<T, Error>;
  7. #[derive(Debug, Clone)]
  8. pub enum Error {
  9. Io(std::io::ErrorKind),
  10. /// VarInt was encoded in a non-minimal way
  11. PathNotFound,
  12. NonMinimalVarInt,
  13. /// Parsing error
  14. ParseFailed(&'static str),
  15. ParseIntError,
  16. ParseFloatError,
  17. AsyncChannelSenderError,
  18. AsyncChannelReceiverError,
  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. SurfHttpError(String),
  56. TomlDeserializeError(String),
  57. TomlSerializeError(String),
  58. CashierNoReply,
  59. Base58EncodeError(String),
  60. Base58DecodeError(String),
  61. ConfigNotFound,
  62. }
  63. impl std::error::Error for Error {}
  64. impl fmt::Display for Error {
  65. fn fmt(&self, f: &mut fmt::Formatter) -> std::fmt::Result {
  66. match *self {
  67. Error::PathNotFound => f.write_str("Cannot find home directory"),
  68. Error::Io(ref err) => write!(f, "io error:{:?}", err),
  69. Error::NonMinimalVarInt => f.write_str("non-minimal varint"),
  70. Error::ParseFailed(ref err) => write!(f, "parse failed: {}", err),
  71. Error::ParseIntError => f.write_str("Parse int error"),
  72. Error::ParseFloatError => f.write_str("Parse float error"),
  73. Error::AsyncChannelSenderError => f.write_str("Async_channel sender error"),
  74. Error::AsyncChannelReceiverError => f.write_str("Async_channel receiver 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 => f.write_str("Not supported network inside cashierd config file"),
  109. Error::BridgeError(ref err) => write!(f, "Bridge error: {}", err),
  110. Error::SerdeJsonError(ref err) => write!(f, "Json serialization error: {}", err),
  111. Error::SurfHttpError(ref err) => write!(f, "Surf Http error: {}", err),
  112. Error::TomlDeserializeError(ref err) => write!(f, "Toml parsing error: {}", err),
  113. Error::TomlSerializeError(ref err) => write!(f, "Toml parsing error: {}", err),
  114. Error::Base58EncodeError(ref err) => write!(f, "bs58 encode error: {}", err),
  115. Error::Base58DecodeError(ref err) => write!(f, "bs58 decode error: {}", err),
  116. Error::CashierNoReply => f.write_str("Cashier did not reply with BTC address"),
  117. Error::ConfigNotFound => {
  118. f.write_str("No config file detected. Please create a config file")
  119. }
  120. }
  121. }
  122. }
  123. impl From<zeromq::ZmqError> for Error {
  124. fn from(err: zeromq::ZmqError) -> Error {
  125. Error::ZmqError(err.to_string())
  126. }
  127. }
  128. impl From<rocksdb::Error> for Error {
  129. fn from(err: rocksdb::Error) -> Error {
  130. Error::RocksdbError(err.to_string())
  131. }
  132. }
  133. impl From<jsonrpc_core::Error> for Error {
  134. fn from(err: jsonrpc_core::Error) -> Error {
  135. Error::JsonRpcError(err.to_string())
  136. }
  137. }
  138. // err.fmt();
  139. impl From<Error> for jsonrpc_core::Error {
  140. fn from(err: Error) -> jsonrpc_core::Error {
  141. jsonrpc_core::Error::invalid_params(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<std::net::AddrParseError> for Error {
  180. fn from(_err: std::net::AddrParseError) -> Error {
  181. Error::AddrParseError
  182. }
  183. }
  184. impl From<std::num::ParseIntError> for Error {
  185. fn from(_err: std::num::ParseIntError) -> Error {
  186. Error::ParseIntError
  187. }
  188. }
  189. impl From<std::num::ParseFloatError> for Error {
  190. fn from(_err: std::num::ParseFloatError) -> Error {
  191. Error::ParseFloatError
  192. }
  193. }
  194. impl From<std::string::FromUtf8Error> for Error {
  195. fn from(_err: std::string::FromUtf8Error) -> Error {
  196. Error::Utf8Error
  197. }
  198. }
  199. impl From<std::str::Utf8Error> for Error {
  200. fn from(err: std::str::Utf8Error) -> Error {
  201. Error::StrUtf8Error(err.to_string())
  202. }
  203. }
  204. impl From<state::VerifyFailed> for Error {
  205. fn from(_err: state::VerifyFailed) -> Error {
  206. Error::VerifyFailed
  207. }
  208. }
  209. impl From<client::ClientFailed> for Error {
  210. fn from(err: client::ClientFailed) -> Error {
  211. Error::ClientFailed(err.to_string())
  212. }
  213. }
  214. #[cfg(feature = "btc")]
  215. impl From<crate::service::BtcFailed> for Error {
  216. fn from(err: crate::service::BtcFailed) -> Error {
  217. Error::BtcFailed(err.to_string())
  218. }
  219. }
  220. #[cfg(feature = "sol")]
  221. impl From<crate::service::SolFailed> for Error {
  222. fn from(err: crate::service::SolFailed) -> Error {
  223. Error::SolFailed(err.to_string())
  224. }
  225. }
  226. impl From<surf::Error> for Error {
  227. fn from(err: surf::Error) -> Error {
  228. Error::SurfHttpError(err.to_string())
  229. }
  230. }
  231. impl From<toml::de::Error> for Error {
  232. fn from(err: toml::de::Error) -> Error {
  233. Error::TomlDeserializeError(err.to_string())
  234. }
  235. }
  236. impl From<toml::ser::Error> for Error {
  237. fn from(err: toml::ser::Error) -> Error {
  238. Error::TomlSerializeError(err.to_string())
  239. }
  240. }
  241. impl From<bs58::encode::Error> for Error {
  242. fn from(err: bs58::encode::Error) -> Error {
  243. Error::Base58EncodeError(err.to_string())
  244. }
  245. }
  246. impl From<bs58::decode::Error> for Error {
  247. fn from(err: bs58::decode::Error) -> Error {
  248. Error::Base58DecodeError(err.to_string())
  249. }
  250. }