error.rs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. use jsonrpc_core::*;
  2. use std::fmt;
  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. AsyncChannelError,
  16. MalformedPacket,
  17. AddrParseError,
  18. BadVariableRefType,
  19. BadOperationType,
  20. BadConstraintType,
  21. InvalidParamName,
  22. MissingParams,
  23. VmError,
  24. BadContract,
  25. Groth16Error,
  26. RusqliteError(String),
  27. OperationFailed,
  28. ConnectFailed,
  29. ConnectTimeout,
  30. ChannelStopped,
  31. ChannelTimeout,
  32. ServiceStopped,
  33. Utf8Error,
  34. StrUtf8Error(String),
  35. NoteDecryptionFailed,
  36. ServicesError(&'static str),
  37. ZmqError(String),
  38. VerifyFailed,
  39. TryIntoError,
  40. TryFromError,
  41. JsonRpcError(String),
  42. RocksdbError(String),
  43. TreeFull,
  44. SerdeJsonError(String),
  45. SurfHttpError(String),
  46. EmptyPassword,
  47. TomlDeserializeError(String),
  48. TomlSerializeError(String),
  49. CashierNoReply,
  50. Base58EncodeError(String),
  51. Base58DecodeError(String),
  52. }
  53. impl std::error::Error for Error {}
  54. impl fmt::Display for Error {
  55. fn fmt(&self, f: &mut fmt::Formatter) -> std::fmt::Result {
  56. match *self {
  57. Error::PathNotFound => f.write_str("Cannot find home directory"),
  58. Error::Io(ref err) => write!(f, "io error:{:?}", err),
  59. Error::NonMinimalVarInt => f.write_str("non-minimal varint"),
  60. Error::ParseFailed(ref err) => write!(f, "parse failed: {}", err),
  61. Error::ParseIntError => f.write_str("Parse int error"),
  62. Error::AsyncChannelError => f.write_str("Async_channel error"),
  63. Error::MalformedPacket => f.write_str("Malformed packet"),
  64. Error::AddrParseError => f.write_str("Unable to parse address"),
  65. Error::BadVariableRefType => f.write_str("Bad variable ref type byte"),
  66. Error::BadOperationType => f.write_str("Bad operation type byte"),
  67. Error::BadConstraintType => f.write_str("Bad constraint type byte"),
  68. Error::InvalidParamName => f.write_str("Invalid param name"),
  69. Error::MissingParams => f.write_str("Missing params"),
  70. Error::VmError => f.write_str("VM error"),
  71. Error::BadContract => f.write_str("Contract is poorly defined"),
  72. Error::Groth16Error => f.write_str("Groth16 error"),
  73. Error::RusqliteError(ref err) => write!(f, "Rusqlite error {}", err),
  74. Error::OperationFailed => f.write_str("Operation failed"),
  75. Error::ConnectFailed => f.write_str("Connection failed"),
  76. Error::ConnectTimeout => f.write_str("Connection timed out"),
  77. Error::ChannelStopped => f.write_str("Channel stopped"),
  78. Error::ChannelTimeout => f.write_str("Channel timed out"),
  79. Error::ServiceStopped => f.write_str("Service stopped"),
  80. Error::Utf8Error => f.write_str("Malformed UTF8"),
  81. Error::StrUtf8Error(ref err) => write!(f, "Malformed UTF8: {}", err),
  82. Error::NoteDecryptionFailed => f.write_str("Unable to decrypt mint note"),
  83. Error::ServicesError(ref err) => write!(f, "Services error: {}", err),
  84. Error::ZmqError(ref err) => write!(f, "ZmqError: {}", err),
  85. Error::VerifyFailed => f.write_str("Verify failed"),
  86. Error::TryIntoError => f.write_str("TryInto error"),
  87. Error::TryFromError => f.write_str("TryFrom error"),
  88. Error::RocksdbError(ref err) => write!(f, "Rocksdb Error: {}", err),
  89. Error::JsonRpcError(ref err) => write!(f, "JsonRpc Error: {}", err),
  90. Error::TreeFull => f.write_str("MerkleTree is full"),
  91. Error::SerdeJsonError(ref err) => write!(f, "Json serialization error: {}", err),
  92. Error::SurfHttpError(ref err) => write!(f, "Surf Http error: {}", err),
  93. Error::EmptyPassword => f.write_str("Password is empty. Cannot create database"),
  94. Error::TomlDeserializeError(ref err) => write!(f, "Toml parsing error: {}", err),
  95. Error::TomlSerializeError(ref err) => write!(f, "Toml parsing error: {}", err),
  96. Error::Base58EncodeError(ref err) => write!(f, "bs58 encode error: {}", err),
  97. Error::Base58DecodeError(ref err) => write!(f, "bs58 decode error: {}", err),
  98. Error::CashierNoReply => f.write_str("Cashier did not reply with BTC address"),
  99. }
  100. }
  101. }
  102. impl From<zeromq::ZmqError> for Error {
  103. fn from(err: zeromq::ZmqError) -> Error {
  104. Error::ZmqError(err.to_string())
  105. }
  106. }
  107. impl From<rocksdb::Error> for Error {
  108. fn from(err: rocksdb::Error) -> Error {
  109. Error::RocksdbError(err.to_string())
  110. }
  111. }
  112. impl From<jsonrpc_core::Error> for Error {
  113. fn from(err: jsonrpc_core::Error) -> Error {
  114. Error::JsonRpcError(err.to_string())
  115. }
  116. }
  117. // err.fmt();
  118. impl From<Error> for jsonrpc_core::Error {
  119. fn from(err: Error) -> jsonrpc_core::Error {
  120. jsonrpc_core::Error::invalid_params(err.to_string())
  121. }
  122. }
  123. impl From<serde_json::Error> for Error {
  124. fn from(err: serde_json::Error) -> Error {
  125. Error::SerdeJsonError(err.to_string())
  126. }
  127. }
  128. impl From<std::io::Error> for Error {
  129. fn from(err: std::io::Error) -> Error {
  130. Error::Io(err.kind())
  131. }
  132. }
  133. impl From<rusqlite::Error> for Error {
  134. fn from(err: rusqlite::Error) -> Error {
  135. Error::RusqliteError(err.to_string())
  136. }
  137. }
  138. impl From<ZkVmError> for Error {
  139. fn from(_err: ZkVmError) -> Error {
  140. Error::VmError
  141. }
  142. }
  143. impl From<bellman::SynthesisError> for Error {
  144. fn from(_err: bellman::SynthesisError) -> Error {
  145. Error::Groth16Error
  146. }
  147. }
  148. impl<T> From<async_channel::SendError<T>> for Error {
  149. fn from(_err: async_channel::SendError<T>) -> Error {
  150. Error::AsyncChannelError
  151. }
  152. }
  153. impl From<async_channel::RecvError> for Error {
  154. fn from(_err: async_channel::RecvError) -> Error {
  155. Error::AsyncChannelError
  156. }
  157. }
  158. impl From<std::net::AddrParseError> for Error {
  159. fn from(_err: std::net::AddrParseError) -> Error {
  160. Error::AddrParseError
  161. }
  162. }
  163. impl From<std::num::ParseIntError> for Error {
  164. fn from(_err: std::num::ParseIntError) -> Error {
  165. Error::ParseIntError
  166. }
  167. }
  168. impl From<std::string::FromUtf8Error> for Error {
  169. fn from(_err: std::string::FromUtf8Error) -> Error {
  170. Error::Utf8Error
  171. }
  172. }
  173. impl From<std::str::Utf8Error> for Error {
  174. fn from(err: std::str::Utf8Error) -> Error {
  175. Error::StrUtf8Error(err.to_string())
  176. }
  177. }
  178. impl From<state::VerifyFailed> for Error {
  179. fn from(_err: state::VerifyFailed) -> Error {
  180. Error::VerifyFailed
  181. }
  182. }
  183. impl From<surf::Error> for Error {
  184. fn from(err: surf::Error) -> Error {
  185. Error::SurfHttpError(err.to_string())
  186. }
  187. }
  188. impl From<toml::de::Error> for Error {
  189. fn from(err: toml::de::Error) -> Error {
  190. Error::TomlDeserializeError(err.to_string())
  191. }
  192. }
  193. impl From<toml::ser::Error> for Error {
  194. fn from(err: toml::ser::Error) -> Error {
  195. Error::TomlSerializeError(err.to_string())
  196. }
  197. }
  198. impl From<bs58::encode::Error> for Error {
  199. fn from(err: bs58::encode::Error) -> Error {
  200. Error::Base58EncodeError(err.to_string())
  201. }
  202. }
  203. impl From<bs58::decode::Error> for Error {
  204. fn from(err: bs58::decode::Error) -> Error {
  205. Error::Base58DecodeError(err.to_string())
  206. }
  207. }