error.rs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. pub type Result<T> = std::result::Result<T, Error>;
  2. #[derive(Debug, Clone, thiserror::Error)]
  3. pub enum Error {
  4. #[error("io error: `{0:?}`")]
  5. Io(std::io::ErrorKind),
  6. #[error("Infallible Error: `{0}`")]
  7. InfallibleError(String),
  8. #[error("Cannot find home directory")]
  9. PathNotFound,
  10. /// VarInt was encoded in a non-minimal way
  11. #[error("non-minimal varint")]
  12. NonMinimalVarInt,
  13. /// Parsing And Encode/Decode errors
  14. #[error("parse failed: `{0}`")]
  15. ParseFailed(&'static str),
  16. #[error("decode failed: `{0}`")]
  17. DecodeError(&'static str),
  18. #[error("encode failed: `{0}`")]
  19. EncodeError(&'static str),
  20. #[error(transparent)]
  21. ParseIntError(#[from] std::num::ParseIntError),
  22. #[error(transparent)]
  23. ParseBigIntError(#[from] num_bigint::ParseBigIntError),
  24. #[error(transparent)]
  25. ParseFloatError(#[from] std::num::ParseFloatError),
  26. #[error(transparent)]
  27. FromHexError(#[from] hex::FromHexError),
  28. #[error("Url parse error `{0}`")]
  29. UrlParseError(String),
  30. #[error("No url found")]
  31. NoUrlFound,
  32. #[error("Malformed packet")]
  33. MalformedPacket,
  34. #[error(transparent)]
  35. AddrParseError(#[from] std::net::AddrParseError),
  36. #[error(transparent)]
  37. Base58EncodeError(#[from] bs58::encode::Error),
  38. #[error(transparent)]
  39. Base58DecodeError(#[from] bs58::decode::Error),
  40. #[error(transparent)]
  41. Utf8Error(#[from] std::string::FromUtf8Error),
  42. #[error(transparent)]
  43. StrUtf8Error(#[from] std::str::Utf8Error),
  44. #[error("TryInto error")]
  45. TryIntoError,
  46. #[error("TryFrom error")]
  47. TryFromError,
  48. #[error(transparent)]
  49. TryFromBigIntError(#[from] num_bigint::TryFromBigIntError<num_bigint::BigUint>),
  50. #[error("Json serialization error: `{0}`")]
  51. SerdeJsonError(String),
  52. #[error(transparent)]
  53. TomlDeserializeError(#[from] toml::de::Error),
  54. #[error(transparent)]
  55. TomlSerializeError(#[from] toml::ser::Error),
  56. #[error("Bincode serialization error: `{0}`")]
  57. BincodeError(String),
  58. /// Contract
  59. #[error("Bad variable ref type byte")]
  60. BadVariableRefType,
  61. #[error("Bad operation type byte")]
  62. BadOperationType,
  63. #[error("Bad constraint type byte")]
  64. BadConstraintType,
  65. #[error("Invalid param name")]
  66. InvalidParamName,
  67. #[error("Invalid param type")]
  68. InvalidParamType,
  69. #[error("Missing params")]
  70. MissingParams,
  71. #[error("Contract is poorly defined")]
  72. BadContract,
  73. #[error("Operation failed")]
  74. OperationFailed,
  75. #[error("PLONK error: `{0}`")]
  76. PlonkError(String),
  77. #[error("Unable to decrypt mint note")]
  78. NoteDecryptionFailed,
  79. #[cfg(feature = "node")]
  80. #[error(transparent)]
  81. VerifyFailed(#[from] crate::node::state::VerifyFailed),
  82. #[error("MerkleTree is full")]
  83. TreeFull,
  84. /// Service
  85. #[error("Services Error: `{0}`")]
  86. ServicesError(&'static str),
  87. #[error("Client failed: `{0}`")]
  88. ClientFailed(String),
  89. #[error("Cashier failed: `{0}`")]
  90. CashierError(String),
  91. #[error("ZmqError: `{0}`")]
  92. ZmqError(String),
  93. /// Database/Sql errors
  94. #[error("Rocksdb error: `{0}`")]
  95. RocksdbError(String),
  96. #[error("sqlx error: `{0}`")]
  97. SqlxError(String),
  98. #[error("SlabsStore Error: `{0}`")]
  99. SlabsStore(String),
  100. /// RPC errors
  101. #[error("JsonRpc Error: `{0}`")]
  102. JsonRpcError(String),
  103. #[error("Not supported network")]
  104. NotSupportedNetwork,
  105. #[error("Not supported token")]
  106. NotSupportedToken,
  107. #[error("Could not parse token parameter")]
  108. TokenParseError,
  109. #[error("Cannot parse network parameter")]
  110. NetworkParseError,
  111. #[error("Async_Native_TLS error: `{0}`")]
  112. AsyncNativeTlsError(String),
  113. #[error("TungsteniteError: `{0}`")]
  114. TungsteniteError(String),
  115. /// Network
  116. #[error("Connection failed")]
  117. ConnectFailed,
  118. #[error("Connection timed out")]
  119. ConnectTimeout,
  120. #[error("Channel stopped")]
  121. ChannelStopped,
  122. #[error("Channel timed out")]
  123. ChannelTimeout,
  124. #[error("Service stopped")]
  125. ServiceStopped,
  126. /// Util
  127. #[error("No config file detected. Please create one.")]
  128. ConfigNotFound,
  129. #[error("No keypair file detected.")]
  130. KeypairPathNotFound,
  131. #[error("SetLoggerError")]
  132. SetLoggerError,
  133. #[error("Async_channel sender error")]
  134. AsyncChannelSenderError,
  135. #[error(transparent)]
  136. AsyncChannelReceiverError(#[from] async_channel::RecvError),
  137. /// Keypari & Address
  138. #[error("Error converting Address to PublicKey")]
  139. AddressToPublicKeyError,
  140. #[error("Error converting bytes to PublicKey")]
  141. PublicKeyFromBytes,
  142. #[error("Error converting bytes to SecretKey")]
  143. SecretKeyFromBytes,
  144. #[error("Invalid Address")]
  145. InvalidAddress,
  146. }
  147. #[cfg(feature = "node")]
  148. impl From<zeromq::ZmqError> for Error {
  149. fn from(err: zeromq::ZmqError) -> Error {
  150. Error::ZmqError(err.to_string())
  151. }
  152. }
  153. #[cfg(feature = "chain")]
  154. impl From<rocksdb::Error> for Error {
  155. fn from(err: rocksdb::Error) -> Error {
  156. Error::RocksdbError(err.to_string())
  157. }
  158. }
  159. #[cfg(feature = "node")]
  160. impl From<sqlx::error::Error> for Error {
  161. fn from(err: sqlx::error::Error) -> Error {
  162. Error::SqlxError(err.to_string())
  163. }
  164. }
  165. impl From<serde_json::Error> for Error {
  166. fn from(err: serde_json::Error) -> Error {
  167. Error::SerdeJsonError(err.to_string())
  168. }
  169. }
  170. impl From<std::io::Error> for Error {
  171. fn from(err: std::io::Error) -> Error {
  172. Error::Io(err.kind())
  173. }
  174. }
  175. impl<T> From<async_channel::SendError<T>> for Error {
  176. fn from(_err: async_channel::SendError<T>) -> Error {
  177. Error::AsyncChannelSenderError
  178. }
  179. }
  180. impl From<async_native_tls::Error> for Error {
  181. fn from(err: async_native_tls::Error) -> Error {
  182. Error::AsyncNativeTlsError(err.to_string())
  183. }
  184. }
  185. impl From<url::ParseError> for Error {
  186. fn from(err: url::ParseError) -> Error {
  187. Error::UrlParseError(err.to_string())
  188. }
  189. }
  190. #[cfg(feature = "node")]
  191. impl From<crate::node::client::ClientFailed> for Error {
  192. fn from(err: crate::node::client::ClientFailed) -> Error {
  193. Error::ClientFailed(err.to_string())
  194. }
  195. }
  196. impl From<log::SetLoggerError> for Error {
  197. fn from(_err: log::SetLoggerError) -> Error {
  198. Error::SetLoggerError
  199. }
  200. }
  201. impl From<tungstenite::Error> for Error {
  202. fn from(err: tungstenite::Error) -> Error {
  203. Error::TungsteniteError(err.to_string())
  204. }
  205. }
  206. impl From<halo2::plonk::Error> for Error {
  207. fn from(err: halo2::plonk::Error) -> Error {
  208. Error::PlonkError(format!("{:?}", err))
  209. }
  210. }
  211. impl From<Box<bincode::ErrorKind>> for Error {
  212. fn from(err: Box<bincode::ErrorKind>) -> Error {
  213. Error::BincodeError(err.to_string())
  214. }
  215. }
  216. impl From<std::convert::Infallible> for Error {
  217. fn from(err: std::convert::Infallible) -> Error {
  218. Error::InfallibleError(err.to_string())
  219. }
  220. }