error.rs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. // Hello developer. Please add your error to the according subsection
  2. // that is commented, or make a new subsection. Keep it clean.
  3. /// Main result type used throughout the codebase.
  4. pub type Result<T> = std::result::Result<T, Error>;
  5. /// Result type used in transaction verifications
  6. pub type VerifyResult<T> = std::result::Result<T, VerifyFailed>;
  7. /// Result type used in the Client module
  8. pub type ClientResult<T> = std::result::Result<T, ClientFailed>;
  9. /// General library errors used throughout the codebase.
  10. #[derive(Debug, Clone, thiserror::Error)]
  11. pub enum Error {
  12. // ==============
  13. // Parsing errors
  14. // ==============
  15. #[error("Parse failed: {0}")]
  16. ParseFailed(&'static str),
  17. #[error(transparent)]
  18. ParseIntError(#[from] std::num::ParseIntError),
  19. #[error(transparent)]
  20. ParseFloatError(#[from] std::num::ParseFloatError),
  21. #[cfg(feature = "num-bigint")]
  22. #[error(transparent)]
  23. ParseBigIntError(#[from] num_bigint::ParseBigIntError),
  24. #[cfg(feature = "num-bigint")]
  25. #[error(transparent)]
  26. TryFromBigIntError(#[from] num_bigint::TryFromBigIntError<num_bigint::BigUint>),
  27. #[cfg(feature = "url")]
  28. #[error(transparent)]
  29. UrlParseError(#[from] url::ParseError),
  30. #[error("URL parse error: {0}")]
  31. UrlParse(String),
  32. #[error(transparent)]
  33. AddrParseError(#[from] std::net::AddrParseError),
  34. #[error("Could not parse token parameter")]
  35. TokenParseError,
  36. // ===============
  37. // Encoding errors
  38. // ===============
  39. #[error("decode failed: {0}")]
  40. DecodeError(&'static str),
  41. #[error("encode failed: {0}")]
  42. EncodeError(&'static str),
  43. #[error("VarInt was encoded in a non-minimal way")]
  44. NonMinimalVarInt,
  45. #[error(transparent)]
  46. Utf8Error(#[from] std::string::FromUtf8Error),
  47. #[error(transparent)]
  48. StrUtf8Error(#[from] std::str::Utf8Error),
  49. #[cfg(feature = "serde_json")]
  50. #[error("serde_json error: {0}")]
  51. SerdeJsonError(String),
  52. #[cfg(feature = "toml")]
  53. #[error(transparent)]
  54. TomlDeserializeError(#[from] toml::de::Error),
  55. #[cfg(feature = "bincode")]
  56. #[error("bincode error: {0}")]
  57. BincodeError(String),
  58. #[cfg(feature = "bs58")]
  59. #[error(transparent)]
  60. Bs58DecodeError(#[from] bs58::decode::Error),
  61. #[error("Bad operation type byte")]
  62. BadOperationType,
  63. // ======================
  64. // Network-related errors
  65. // ======================
  66. #[error("Unsupported network transport: {0}")]
  67. UnsupportedTransport(String),
  68. #[error("Transport error: {0}")]
  69. TransportError(String),
  70. #[error("Connection failed")]
  71. ConnectFailed,
  72. #[error("Connection timed out")]
  73. ConnectTimeout,
  74. #[error("Channel stopped")]
  75. ChannelStopped,
  76. #[error("Channel timed out")]
  77. ChannelTimeout,
  78. #[error("Service stopped")]
  79. ServiceStopped,
  80. #[error("Operation failed")]
  81. OperationFailed,
  82. #[error("Malformed packet")]
  83. MalformedPacket,
  84. #[error("Socks proxy error: {0}")]
  85. SocksError(String),
  86. #[error("No Socks5 URL found")]
  87. NoSocks5UrlFound,
  88. #[error("No URL found")]
  89. NoUrlFound,
  90. #[cfg(feature = "tungstenite")]
  91. #[error("tungstenite error: {0}")]
  92. TungsteniteError(String),
  93. #[cfg(feature = "async-native-tls")]
  94. #[error("async_native_tls error: {0}")]
  95. AsyncNativeTlsError(String),
  96. // =============
  97. // Crypto errors
  98. // =============
  99. #[cfg(feature = "halo2_proofs")]
  100. #[error("halo2 plonk error: {0}")]
  101. PlonkError(String),
  102. #[error("Unable to decrypt mint note")]
  103. NoteDecryptionFailed,
  104. #[error("No keypair file detected")]
  105. KeypairPathNotFound,
  106. #[error("Failed converting bytes to PublicKey")]
  107. PublicKeyFromBytes,
  108. #[error("Failed converting bytes to SecretKey")]
  109. SecretKeyFromBytes,
  110. #[error("Failed converting b58 string to PublicKey")]
  111. PublicKeyFromStr,
  112. #[error("Failed converting bs58 string to SecretKey")]
  113. SecretKeyFromStr,
  114. #[error("Invalid DarkFi address")]
  115. InvalidAddress,
  116. // =======================
  117. // Protocol-related errors
  118. // =======================
  119. #[error("Unsupported chain")]
  120. UnsupportedChain,
  121. #[error("Unsupported token")]
  122. UnsupportedToken,
  123. #[error("Unsupported coin network")]
  124. UnsupportedCoinNetwork,
  125. #[error("Raft error: {0}")]
  126. RaftError(String),
  127. // ===============
  128. // Database errors
  129. // ===============
  130. #[cfg(feature = "sqlx")]
  131. #[error("Sqlx error: {0}")]
  132. SqlxError(String),
  133. #[cfg(feature = "sled")]
  134. #[error(transparent)]
  135. SledError(#[from] sled::Error),
  136. #[error("Transaction {0} not found in database")]
  137. TransactionNotFound(String),
  138. #[error("Block {0} not found in database")]
  139. BlockNotFound(String),
  140. #[error("Block in slot {0} not found in database")]
  141. SlotNotFound(u64),
  142. #[error("Block {0} metadata not found in database")]
  143. BlockMetadataNotFound(String),
  144. // =============
  145. // Wallet errors
  146. // =============
  147. #[error("Wallet password is empty")]
  148. WalletEmptyPassword,
  149. #[error("Merkle tree already exists in wallet")]
  150. WalletTreeExists,
  151. // ===================
  152. // wasm runtime errors
  153. // ===================
  154. #[cfg(feature = "wasm-runtime")]
  155. #[error("Wasmer compile error: {0}")]
  156. WasmerCompileError(String),
  157. #[cfg(feature = "wasm-runtime")]
  158. #[error("Wasmer export error: {0}")]
  159. WasmerExportError(String),
  160. #[cfg(feature = "wasm-runtime")]
  161. #[error("Wasmer runtime error: {0}")]
  162. WasmerRuntimeError(String),
  163. #[cfg(feature = "wasm-runtime")]
  164. #[error("Wasmer instantiation error: {0}")]
  165. WasmerInstantiationError(String),
  166. #[cfg(feature = "wasm-runtime")]
  167. #[error("wasm runtime out of memory")]
  168. WasmerOomError,
  169. // ====================
  170. // Miscellaneous errors
  171. // ====================
  172. #[error("IO error: {0}")]
  173. Io(std::io::ErrorKind),
  174. #[error("Infallible error: {0}")]
  175. InfallibleError(String),
  176. #[cfg(feature = "async-channel")]
  177. #[error("async_channel sender error: {0}")]
  178. AsyncChannelSendError(String),
  179. #[cfg(feature = "async-channel")]
  180. #[error("async_channel receiver error: {0}")]
  181. AsyncChannelRecvError(String),
  182. #[error("SetLogger (log crate) failed: {0}")]
  183. SetLoggerError(String),
  184. #[error("ValueIsNotObject")]
  185. ValueIsNotObject,
  186. #[error("No config file detected")]
  187. ConfigNotFound,
  188. #[error("Failed decoding bincode: {0}")]
  189. ZkasDecoderError(&'static str),
  190. // ==============================================
  191. // Wrappers for other error types in this library
  192. // ==============================================
  193. #[error(transparent)]
  194. VerifyFailed(#[from] VerifyFailed),
  195. #[error(transparent)]
  196. ClientFailed(#[from] ClientFailed),
  197. }
  198. /// Transaction verification errors
  199. #[derive(Debug, Clone, thiserror::Error)]
  200. pub enum VerifyFailed {
  201. #[error("Invalid cashier/faucet public key for clear input {0}")]
  202. InvalidCashierOrFaucetKey(usize),
  203. #[error("Invalid Merkle root for input {0}")]
  204. InvalidMerkle(usize),
  205. #[error("Invalid signature for input {0}")]
  206. InputSignature(usize),
  207. #[error("Invalid signature for clear input {0}")]
  208. ClearInputSignature(usize),
  209. #[error("Token commitments in inputs or outputs to not match")]
  210. TokenMismatch,
  211. #[error("Money in does not match money out (value commitments)")]
  212. MissingFunds,
  213. #[error("Mint proof verification failure for input {0}")]
  214. MintProof(usize),
  215. #[error("Burn proof verification failure for input {0}")]
  216. BurnProof(usize),
  217. #[error("Internal error: {0}")]
  218. InternalError(String),
  219. }
  220. /// Client module errors
  221. #[derive(Debug, Clone, thiserror::Error)]
  222. pub enum ClientFailed {
  223. #[error("Not enough value: {0}")]
  224. NotEnoughValue(u64),
  225. #[error("Invalid address: {0}")]
  226. InvalidAddress(String),
  227. #[error("Invalid amount: {0}")]
  228. InvalidAmount(u64),
  229. #[error("Internal error: {0}")]
  230. InternalError(String),
  231. #[error("Verify error: {0}")]
  232. VerifyError(String),
  233. }
  234. impl From<Error> for VerifyFailed {
  235. fn from(err: Error) -> Self {
  236. Self::InternalError(err.to_string())
  237. }
  238. }
  239. impl From<Error> for ClientFailed {
  240. fn from(err: Error) -> Self {
  241. Self::InternalError(err.to_string())
  242. }
  243. }
  244. impl From<VerifyFailed> for ClientFailed {
  245. fn from(err: VerifyFailed) -> Self {
  246. Self::VerifyError(err.to_string())
  247. }
  248. }
  249. #[cfg(feature = "net2")]
  250. impl<T: std::fmt::Display> From<crate::net2::transport::TransportError<T>> for Error {
  251. fn from(err: crate::net2::transport::TransportError<T>) -> Self {
  252. Self::TransportError(err.to_string())
  253. }
  254. }
  255. impl From<std::io::Error> for Error {
  256. fn from(err: std::io::Error) -> Self {
  257. Self::Io(err.kind())
  258. }
  259. }
  260. impl From<std::convert::Infallible> for Error {
  261. fn from(err: std::convert::Infallible) -> Self {
  262. Self::InfallibleError(err.to_string())
  263. }
  264. }
  265. impl From<()> for Error {
  266. fn from(_err: ()) -> Self {
  267. Self::InfallibleError("Infallible".into())
  268. }
  269. }
  270. #[cfg(feature = "async-channel")]
  271. impl<T> From<async_channel::SendError<T>> for Error {
  272. fn from(err: async_channel::SendError<T>) -> Self {
  273. Self::AsyncChannelSendError(err.to_string())
  274. }
  275. }
  276. #[cfg(feature = "async-channel")]
  277. impl From<async_channel::RecvError> for Error {
  278. fn from(err: async_channel::RecvError) -> Self {
  279. Self::AsyncChannelRecvError(err.to_string())
  280. }
  281. }
  282. #[cfg(feature = "async-native-tls")]
  283. impl From<async_native_tls::Error> for Error {
  284. fn from(err: async_native_tls::Error) -> Self {
  285. Self::AsyncNativeTlsError(err.to_string())
  286. }
  287. }
  288. impl From<log::SetLoggerError> for Error {
  289. fn from(err: log::SetLoggerError) -> Self {
  290. Self::SetLoggerError(err.to_string())
  291. }
  292. }
  293. #[cfg(feature = "sqlx")]
  294. impl From<sqlx::error::Error> for Error {
  295. fn from(err: sqlx::error::Error) -> Self {
  296. Self::SqlxError(err.to_string())
  297. }
  298. }
  299. #[cfg(feature = "halo2_proofs")]
  300. impl From<halo2_proofs::plonk::Error> for Error {
  301. fn from(err: halo2_proofs::plonk::Error) -> Self {
  302. Self::PlonkError(err.to_string())
  303. }
  304. }
  305. #[cfg(feature = "tungstenite")]
  306. impl From<tungstenite::Error> for Error {
  307. fn from(err: tungstenite::Error) -> Self {
  308. Self::TungsteniteError(err.to_string())
  309. }
  310. }
  311. #[cfg(feature = "bincode")]
  312. impl From<bincode::ErrorKind> for Error {
  313. fn from(err: bincode::ErrorKind) -> Self {
  314. Self::BincodeError(err.to_string())
  315. }
  316. }
  317. #[cfg(feature = "bincode")]
  318. impl From<Box<bincode::ErrorKind>> for Error {
  319. fn from(err: Box<bincode::ErrorKind>) -> Self {
  320. Self::BincodeError(err.to_string())
  321. }
  322. }
  323. #[cfg(feature = "serde_json")]
  324. impl From<serde_json::Error> for Error {
  325. fn from(err: serde_json::Error) -> Self {
  326. Self::SerdeJsonError(err.to_string())
  327. }
  328. }
  329. #[cfg(feature = "fast-socks5")]
  330. impl From<fast_socks5::SocksError> for Error {
  331. fn from(err: fast_socks5::SocksError) -> Self {
  332. Self::SocksError(err.to_string())
  333. }
  334. }
  335. #[cfg(feature = "wasm-runtime")]
  336. impl From<wasmer::CompileError> for Error {
  337. fn from(err: wasmer::CompileError) -> Self {
  338. Self::WasmerCompileError(err.to_string())
  339. }
  340. }
  341. #[cfg(feature = "wasm-runtime")]
  342. impl From<wasmer::ExportError> for Error {
  343. fn from(err: wasmer::ExportError) -> Self {
  344. Self::WasmerExportError(err.to_string())
  345. }
  346. }
  347. #[cfg(feature = "wasm-runtime")]
  348. impl From<wasmer::RuntimeError> for Error {
  349. fn from(err: wasmer::RuntimeError) -> Self {
  350. Self::WasmerRuntimeError(err.to_string())
  351. }
  352. }
  353. #[cfg(feature = "wasm-runtime")]
  354. impl From<wasmer::InstantiationError> for Error {
  355. fn from(err: wasmer::InstantiationError) -> Self {
  356. Self::WasmerInstantiationError(err.to_string())
  357. }
  358. }