error.rs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2023 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. // Hello developer. Please add your error to the according subsection
  19. // that is commented, or make a new subsection. Keep it clean.
  20. /// Main result type used throughout the codebase.
  21. pub type Result<T> = std::result::Result<T, Error>;
  22. /// Result type used in the Client module
  23. pub type ClientResult<T> = std::result::Result<T, ClientFailed>;
  24. /// General library errors used throughout the codebase.
  25. #[derive(Debug, Clone, thiserror::Error)]
  26. pub enum Error {
  27. // ==============
  28. // Parsing errors
  29. // ==============
  30. #[error("Parse failed: {0}")]
  31. ParseFailed(&'static str),
  32. #[error(transparent)]
  33. ParseIntError(#[from] std::num::ParseIntError),
  34. #[error(transparent)]
  35. ParseFloatError(#[from] std::num::ParseFloatError),
  36. #[cfg(feature = "url")]
  37. #[error(transparent)]
  38. UrlParseError(#[from] url::ParseError),
  39. #[error("URL parse error: {0}")]
  40. UrlParse(String),
  41. #[error(transparent)]
  42. AddrParseError(#[from] std::net::AddrParseError),
  43. #[error("Could not parse token parameter")]
  44. TokenParseError,
  45. #[error(transparent)]
  46. TryFromSliceError(#[from] std::array::TryFromSliceError),
  47. #[cfg(feature = "dashu")]
  48. #[error(transparent)]
  49. DashuConversionError(#[from] dashu::base::error::ConversionError),
  50. #[cfg(feature = "dashu")]
  51. #[error(transparent)]
  52. DashuParseError(#[from] dashu::base::error::ParseError),
  53. #[cfg(feature = "semver")]
  54. #[error("semver parse error: {0}")]
  55. SemverError(String),
  56. // ===============
  57. // Encoding errors
  58. // ===============
  59. #[error("decode failed: {0}")]
  60. DecodeError(&'static str),
  61. #[error("encode failed: {0}")]
  62. EncodeError(&'static str),
  63. #[error("VarInt was encoded in a non-minimal way")]
  64. NonMinimalVarInt,
  65. #[error(transparent)]
  66. Utf8Error(#[from] std::string::FromUtf8Error),
  67. #[error(transparent)]
  68. StrUtf8Error(#[from] std::str::Utf8Error),
  69. #[cfg(feature = "serde_json")]
  70. #[error("serde_json error: {0}")]
  71. SerdeJsonError(String),
  72. #[cfg(feature = "tinyjson")]
  73. #[error("JSON parse error: {0}")]
  74. JsonParseError(String),
  75. #[cfg(feature = "tinyjson")]
  76. #[error("JSON generate error: {0}")]
  77. JsonGenerateError(String),
  78. #[cfg(feature = "toml")]
  79. #[error(transparent)]
  80. TomlDeserializeError(#[from] toml::de::Error),
  81. #[cfg(feature = "bs58")]
  82. #[error(transparent)]
  83. Bs58DecodeError(#[from] bs58::decode::Error),
  84. #[error("Bad operation type byte")]
  85. BadOperationType,
  86. // ======================
  87. // Network-related errors
  88. // ======================
  89. #[error("Invalid Dialer scheme")]
  90. InvalidDialerScheme,
  91. #[error("Invalid Listener scheme")]
  92. InvalidListenerScheme,
  93. #[error("Unsupported network transport: {0}")]
  94. UnsupportedTransport(String),
  95. #[error("Unsupported network transport upgrade: {0}")]
  96. UnsupportedTransportUpgrade(String),
  97. #[error("Connection failed")]
  98. ConnectFailed,
  99. #[cfg(feature = "system")]
  100. #[error(transparent)]
  101. TimeoutError(#[from] crate::system::timeout::TimeoutError),
  102. #[error("Connection timed out")]
  103. ConnectTimeout,
  104. #[error("Channel stopped")]
  105. ChannelStopped,
  106. #[error("Channel timed out")]
  107. ChannelTimeout,
  108. #[error("Failed to reach any seeds")]
  109. SeedFailed,
  110. #[error("Network service stopped")]
  111. NetworkServiceStopped,
  112. #[error("Create listener bound to {0} failed")]
  113. BindFailed(String),
  114. #[error("Accept a new incoming connection from the listener {0} failed")]
  115. AcceptConnectionFailed(String),
  116. #[error("Accept a new tls connection from the listener {0} failed")]
  117. AcceptTlsConnectionFailed(String),
  118. #[error("Network operation failed")]
  119. NetworkOperationFailed,
  120. #[error("Missing P2P message dispatcher")]
  121. MissingDispatcher,
  122. #[cfg(feature = "arti-client")]
  123. #[error(transparent)]
  124. ArtiError(#[from] arti_client::Error),
  125. #[error("Malformed packet")]
  126. MalformedPacket,
  127. #[error("Socks proxy error: {0}")]
  128. SocksError(String),
  129. #[error("No Socks5 URL found")]
  130. NoSocks5UrlFound,
  131. #[error("No URL found")]
  132. NoUrlFound,
  133. #[cfg(feature = "async-tungstenite")]
  134. #[error("tungstenite error: {0}")]
  135. TungsteniteError(String),
  136. #[error("Tor error: {0}")]
  137. TorError(String),
  138. #[error("Node is not connected to other nodes.")]
  139. NetworkNotConnected,
  140. #[error("P2P network stopped")]
  141. P2PNetworkStopped,
  142. // =============
  143. // Crypto errors
  144. // =============
  145. #[cfg(feature = "halo2_proofs")]
  146. #[error("halo2 plonk error: {0}")]
  147. PlonkError(String),
  148. #[error("Unable to decrypt mint note: {0}")]
  149. NoteDecryptionFailed(String),
  150. #[error("No keypair file detected")]
  151. KeypairPathNotFound,
  152. #[error("Failed converting bytes to PublicKey")]
  153. PublicKeyFromBytes,
  154. #[error("Failed converting bytes to Coin")]
  155. CoinFromBytes,
  156. #[error("Failed converting bytes to SecretKey")]
  157. SecretKeyFromBytes,
  158. #[error("Failed converting b58 string to PublicKey")]
  159. PublicKeyFromStr,
  160. #[error("Failed converting bs58 string to SecretKey")]
  161. SecretKeyFromStr,
  162. #[error("Invalid DarkFi address")]
  163. InvalidAddress,
  164. #[cfg(feature = "async-rustls")]
  165. #[error(transparent)]
  166. RustlsError(#[from] async_rustls::rustls::Error),
  167. #[cfg(feature = "async-rustls")]
  168. #[error("Invalid DNS Name {0}")]
  169. RustlsInvalidDns(String),
  170. #[error("unable to decrypt rcpt")]
  171. TxRcptDecryptionError,
  172. #[cfg(feature = "blake3")]
  173. #[error(transparent)]
  174. Blake3FromHexError(#[from] blake3::HexError),
  175. // =======================
  176. // Protocol-related errors
  177. // =======================
  178. #[error("Unsupported chain")]
  179. UnsupportedChain,
  180. #[error("JSON-RPC error: {0:?}")]
  181. JsonRpcError((i32, String)),
  182. #[cfg(feature = "rpc")]
  183. #[error(transparent)]
  184. RpcServerError(RpcError),
  185. #[cfg(feature = "rpc")]
  186. #[error("JSON-RPC connections exhausted")]
  187. RpcConnectionsExhausted,
  188. #[cfg(feature = "rpc")]
  189. #[error("JSON-RPC server stopped")]
  190. RpcServerStopped,
  191. #[cfg(feature = "rpc")]
  192. #[error("JSON-RPC client stopped")]
  193. RpcClientStopped,
  194. #[error("Unexpected JSON-RPC data received: {0}")]
  195. UnexpectedJsonRpc(String),
  196. #[error("Received proposal from unknown node")]
  197. UnknownNodeError,
  198. #[error("Public inputs are invalid")]
  199. InvalidPublicInputsError,
  200. #[error("Coin is not the slot block producer")]
  201. CoinIsNotSlotProducer,
  202. #[error("Error during leader proof verification")]
  203. LeaderProofVerification,
  204. #[error("Signature could not be verified")]
  205. InvalidSignature,
  206. #[error("State transition failed")]
  207. StateTransitionError,
  208. #[error("No forks exist")]
  209. ForksNotFound,
  210. #[error("Check if proposal extends any existing fork chains failed")]
  211. ExtendedChainIndexNotFound,
  212. #[error("Proposal received after finalization sync period")]
  213. ProposalAfterFinalizationError,
  214. #[error("Proposal received not for current slot")]
  215. ProposalNotForCurrentSlotError,
  216. #[error("Proposal contains missmatched hashes")]
  217. ProposalHashesMissmatchError,
  218. #[error("Proposal contains unknown slots")]
  219. ProposalContainsUnknownSlots,
  220. #[error("Proposal contains missmatched headers")]
  221. ProposalHeadersMissmatchError,
  222. #[error("Proposal contains different coin creation eta")]
  223. ProposalDifferentCoinEtaError,
  224. #[error("Proposal contains spent coin")]
  225. ProposalIsSpent,
  226. #[error("Proposal contains more transactions than configured cap")]
  227. ProposalTxsExceedCapError,
  228. #[error("Unable to verify transfer transaction")]
  229. TransferTxVerification,
  230. #[error("Unable to verify proposed mu values")]
  231. ProposalPublicValuesMismatched,
  232. #[error("Proposer is not eligible to produce proposals")]
  233. ProposalProposerNotEligible,
  234. #[error("Erroneous transactions detected")]
  235. ErroneousTxsDetected,
  236. #[error("Proposal task stopped")]
  237. ProposalTaskStopped,
  238. #[error("Miner task stopped")]
  239. MinerTaskStopped,
  240. #[error("Calculated total work is zero")]
  241. PoWTotalWorkIsZero,
  242. #[error("Erroneous cutoff calculation")]
  243. PoWCuttofCalculationError,
  244. #[error("Provided timestamp is invalid")]
  245. PoWInvalidTimestamp,
  246. #[error("Provided output hash is greater than current target")]
  247. PoWInvalidOutHash,
  248. // ===============
  249. // Database errors
  250. // ===============
  251. #[cfg(feature = "rusqlite")]
  252. #[error("rusqlite error: {0}")]
  253. RusqliteError(String),
  254. #[cfg(feature = "sled")]
  255. #[error(transparent)]
  256. SledError(#[from] sled::Error),
  257. #[cfg(feature = "sled")]
  258. #[error(transparent)]
  259. SledTransactionError(#[from] sled::transaction::TransactionError),
  260. #[error("Transaction {0} not found in database")]
  261. TransactionNotFound(String),
  262. #[error("Transaction already seen")]
  263. TransactionAlreadySeen,
  264. #[error("Input vectors have different length")]
  265. InvalidInputLengths,
  266. #[error("Header {0} not found in database")]
  267. HeaderNotFound(String),
  268. #[error("Block {0} is invalid")]
  269. BlockIsInvalid(String),
  270. #[error("Block version {0} is invalid")]
  271. BlockVersionIsInvalid(u8),
  272. #[error("Block {0} already in database")]
  273. BlockAlreadyExists(String),
  274. #[error("Block {0} not found in database")]
  275. BlockNotFound(String),
  276. #[error("Block with order number {0} not found in database")]
  277. BlockNumberNotFound(u64),
  278. #[error("Block difficulty for height number {0} not found in database")]
  279. BlockDifficultyNotFound(u64),
  280. #[error("Block {0} contains 0 transactions")]
  281. BlockContainsNoTransactions(String),
  282. #[error("Verifying slot missmatch")]
  283. VerifyingSlotMissmatch(),
  284. #[error("Slot {0} is invalid")]
  285. SlotIsInvalid(u64),
  286. #[error("Slot {0} not found in database")]
  287. SlotNotFound(u64),
  288. #[error("Block {0} slots not found in database")]
  289. BlockSlotsNotFound(String),
  290. #[error("Future slot {0} was received")]
  291. FutureSlotReceived(u64),
  292. #[error("Contract {0} not found in database")]
  293. ContractNotFound(String),
  294. #[error("Contract state tree not found")]
  295. ContractStateNotFound,
  296. #[error("Contract already initialized")]
  297. ContractAlreadyInitialized,
  298. #[error("zkas bincode not found in sled database")]
  299. ZkasBincodeNotFound,
  300. // =============
  301. // Wallet errors
  302. // =============
  303. #[error("Wallet password is empty")]
  304. WalletEmptyPassword,
  305. #[error("Merkle tree already exists in wallet")]
  306. WalletTreeExists,
  307. #[error("Wallet insufficient balance")]
  308. WalletInsufficientBalance,
  309. // ===================
  310. // wasm runtime errors
  311. // ===================
  312. #[cfg(feature = "wasm-runtime")]
  313. #[error("Wasmer compile error: {0}")]
  314. WasmerCompileError(String),
  315. #[cfg(feature = "wasm-runtime")]
  316. #[error("Wasmer export error: {0}")]
  317. WasmerExportError(String),
  318. #[cfg(feature = "wasm-runtime")]
  319. #[error("Wasmer runtime error: {0}")]
  320. WasmerRuntimeError(String),
  321. #[cfg(feature = "wasm-runtime")]
  322. #[error("Wasmer instantiation error: {0}")]
  323. WasmerInstantiationError(String),
  324. #[cfg(feature = "wasm-runtime")]
  325. #[error("wasm memory error")]
  326. WasmerMemoryError(String),
  327. #[cfg(feature = "wasm-runtime")]
  328. #[error("wasm runtime out of memory")]
  329. WasmerOomError(String),
  330. #[cfg(feature = "darkfi-sdk")]
  331. #[error("Contract execution failed")]
  332. ContractError(darkfi_sdk::error::ContractError),
  333. #[cfg(feature = "blockchain")]
  334. #[error("contract wasm bincode not found")]
  335. WasmBincodeNotFound,
  336. #[cfg(feature = "wasm-runtime")]
  337. #[error("contract initialize error")]
  338. ContractInitError(u64),
  339. #[cfg(feature = "wasm-runtime")]
  340. #[error("contract execution error")]
  341. ContractExecError(u64),
  342. // ====================
  343. // Event Graph errors
  344. // ====================
  345. #[error("Event is not found in tree: {0}")]
  346. EventNotFound(String),
  347. // ====================
  348. // Miscellaneous errors
  349. // ====================
  350. #[error("IO error: {0}")]
  351. Io(std::io::ErrorKind),
  352. #[error("Infallible error: {0}")]
  353. InfallibleError(String),
  354. #[cfg(feature = "smol")]
  355. #[error("async_channel sender error: {0}")]
  356. AsyncChannelSendError(String),
  357. #[cfg(feature = "smol")]
  358. #[error("async_channel receiver error: {0}")]
  359. AsyncChannelRecvError(String),
  360. #[error("SetLogger (log crate) failed: {0}")]
  361. SetLoggerError(String),
  362. #[error("ValueIsNotObject")]
  363. ValueIsNotObject,
  364. #[error("No config file detected")]
  365. ConfigNotFound,
  366. #[error("Invalid config file detected")]
  367. ConfigInvalid,
  368. #[error("Failed decoding bincode: {0}")]
  369. ZkasDecoderError(String),
  370. #[cfg(feature = "util")]
  371. #[error("System clock is not correct!")]
  372. InvalidClock,
  373. #[error("Unsupported OS")]
  374. UnsupportedOS,
  375. #[error("System clock went backwards")]
  376. BackwardsTime(std::time::SystemTimeError),
  377. #[error("Detached task stopped")]
  378. DetachedTaskStopped,
  379. // ==============================================
  380. // Wrappers for other error types in this library
  381. // ==============================================
  382. #[error(transparent)]
  383. ClientFailed(#[from] ClientFailed),
  384. #[cfg(feature = "tx")]
  385. #[error(transparent)]
  386. TxVerifyFailed(#[from] TxVerifyFailed),
  387. //=============
  388. // clock
  389. //=============
  390. #[error("clock out of sync with peers: {0}")]
  391. ClockOutOfSync(String),
  392. // ================
  393. // DHT/Geode errors
  394. // ================
  395. #[error("Geode needs garbage collection")]
  396. GeodeNeedsGc,
  397. #[error("Geode file not found")]
  398. GeodeFileNotFound,
  399. #[error("Geode chunk not found")]
  400. GeodeChunkNotFound,
  401. #[error("Geode file route not found")]
  402. GeodeFileRouteNotFound,
  403. #[error("Geode chunk route not found")]
  404. GeodeChunkRouteNotFound,
  405. // ==================
  406. // Event Graph errors
  407. // ==================
  408. #[error("DAG sync failed")]
  409. DagSyncFailed,
  410. // =========
  411. // Catch-all
  412. // =========
  413. #[error("{0}")]
  414. Custom(String),
  415. }
  416. #[cfg(feature = "tx")]
  417. impl Error {
  418. /// Auxiliary function to retrieve the vector of erroneous
  419. /// transactions from a TxVerifyFailed error.
  420. /// In any other case, we return the error itself.
  421. pub fn retrieve_erroneous_txs(&self) -> Result<Vec<crate::tx::Transaction>> {
  422. if let Self::TxVerifyFailed(TxVerifyFailed::ErroneousTxs(erroneous_txs)) = self {
  423. return Ok(erroneous_txs.clone())
  424. };
  425. Err(self.clone())
  426. }
  427. }
  428. #[cfg(feature = "tx")]
  429. /// Transaction verification errors
  430. #[derive(Debug, Clone, thiserror::Error)]
  431. pub enum TxVerifyFailed {
  432. #[error("Transaction {0} already exists")]
  433. AlreadySeenTx(String),
  434. #[error("Invalid transaction signature")]
  435. InvalidSignature,
  436. #[error("Missing signatures in transaction")]
  437. MissingSignatures,
  438. #[error("Missing contract calls in transaction")]
  439. MissingCalls,
  440. #[error("Missing Money::Fee call in transaction")]
  441. MissingFee,
  442. #[error("Invalid ZK proof in transaction")]
  443. InvalidZkProof,
  444. #[error("Erroneous transactions found")]
  445. ErroneousTxs(Vec<crate::tx::Transaction>),
  446. }
  447. /// Client module errors
  448. #[derive(Debug, Clone, thiserror::Error)]
  449. pub enum ClientFailed {
  450. #[error("IO error: {0}")]
  451. Io(std::io::ErrorKind),
  452. #[error("Not enough value: {0}")]
  453. NotEnoughValue(u64),
  454. #[error("Invalid address: {0}")]
  455. InvalidAddress(String),
  456. #[error("Invalid amount: {0}")]
  457. InvalidAmount(u64),
  458. #[error("Invalid token ID: {0}")]
  459. InvalidTokenId(String),
  460. #[error("Internal error: {0}")]
  461. InternalError(String),
  462. #[error("Verify error: {0}")]
  463. VerifyError(String),
  464. }
  465. #[cfg(feature = "rpc")]
  466. #[derive(Clone, Debug, thiserror::Error)]
  467. pub enum RpcError {
  468. #[error("Connection closed: {0}")]
  469. ConnectionClosed(String),
  470. #[error("Invalid JSON: {0}")]
  471. InvalidJson(String),
  472. #[error("IO Error: {0}")]
  473. IoError(std::io::ErrorKind),
  474. }
  475. #[cfg(feature = "rpc")]
  476. impl From<std::io::Error> for RpcError {
  477. fn from(err: std::io::Error) -> Self {
  478. Self::IoError(err.kind())
  479. }
  480. }
  481. #[cfg(feature = "rpc")]
  482. impl From<RpcError> for Error {
  483. fn from(err: RpcError) -> Self {
  484. Self::RpcServerError(err)
  485. }
  486. }
  487. impl From<Error> for ClientFailed {
  488. fn from(err: Error) -> Self {
  489. Self::InternalError(err.to_string())
  490. }
  491. }
  492. impl From<std::io::Error> for ClientFailed {
  493. fn from(err: std::io::Error) -> Self {
  494. Self::Io(err.kind())
  495. }
  496. }
  497. impl From<std::io::Error> for Error {
  498. fn from(err: std::io::Error) -> Self {
  499. Self::Io(err.kind())
  500. }
  501. }
  502. impl From<std::time::SystemTimeError> for Error {
  503. fn from(err: std::time::SystemTimeError) -> Self {
  504. Self::BackwardsTime(err)
  505. }
  506. }
  507. impl From<std::convert::Infallible> for Error {
  508. fn from(err: std::convert::Infallible) -> Self {
  509. Self::InfallibleError(err.to_string())
  510. }
  511. }
  512. impl From<()> for Error {
  513. fn from(_err: ()) -> Self {
  514. Self::InfallibleError("Infallible".into())
  515. }
  516. }
  517. #[cfg(feature = "smol")]
  518. impl<T> From<smol::channel::SendError<T>> for Error {
  519. fn from(err: smol::channel::SendError<T>) -> Self {
  520. Self::AsyncChannelSendError(err.to_string())
  521. }
  522. }
  523. #[cfg(feature = "smol")]
  524. impl From<smol::channel::RecvError> for Error {
  525. fn from(err: smol::channel::RecvError) -> Self {
  526. Self::AsyncChannelRecvError(err.to_string())
  527. }
  528. }
  529. impl From<log::SetLoggerError> for Error {
  530. fn from(err: log::SetLoggerError) -> Self {
  531. Self::SetLoggerError(err.to_string())
  532. }
  533. }
  534. #[cfg(feature = "rusqlite")]
  535. impl From<rusqlite::Error> for Error {
  536. fn from(err: rusqlite::Error) -> Self {
  537. Self::RusqliteError(err.to_string())
  538. }
  539. }
  540. #[cfg(feature = "halo2_proofs")]
  541. impl From<halo2_proofs::plonk::Error> for Error {
  542. fn from(err: halo2_proofs::plonk::Error) -> Self {
  543. Self::PlonkError(err.to_string())
  544. }
  545. }
  546. #[cfg(feature = "semver")]
  547. impl From<semver::Error> for Error {
  548. fn from(err: semver::Error) -> Self {
  549. Self::SemverError(err.to_string())
  550. }
  551. }
  552. /*
  553. #[cfg(feature = "tungstenite")]
  554. impl From<tungstenite::Error> for Error {
  555. fn from(err: tungstenite::Error) -> Self {
  556. Self::TungsteniteError(err.to_string())
  557. }
  558. }
  559. */
  560. #[cfg(feature = "async-tungstenite")]
  561. impl From<async_tungstenite::tungstenite::Error> for Error {
  562. fn from(err: async_tungstenite::tungstenite::Error) -> Self {
  563. Self::TungsteniteError(err.to_string())
  564. }
  565. }
  566. #[cfg(feature = "async-rustls")]
  567. impl From<async_rustls::rustls::client::InvalidDnsNameError> for Error {
  568. fn from(err: async_rustls::rustls::client::InvalidDnsNameError) -> Self {
  569. Self::RustlsInvalidDns(err.to_string())
  570. }
  571. }
  572. #[cfg(feature = "serde_json")]
  573. impl From<serde_json::Error> for Error {
  574. fn from(err: serde_json::Error) -> Self {
  575. Self::SerdeJsonError(err.to_string())
  576. }
  577. }
  578. #[cfg(feature = "tinyjson")]
  579. impl From<tinyjson::JsonParseError> for Error {
  580. fn from(err: tinyjson::JsonParseError) -> Self {
  581. Self::JsonParseError(err.to_string())
  582. }
  583. }
  584. #[cfg(feature = "tinyjson")]
  585. impl From<tinyjson::JsonGenerateError> for Error {
  586. fn from(err: tinyjson::JsonGenerateError) -> Self {
  587. Self::JsonGenerateError(err.to_string())
  588. }
  589. }
  590. #[cfg(feature = "fast-socks5")]
  591. impl From<fast_socks5::SocksError> for Error {
  592. fn from(err: fast_socks5::SocksError) -> Self {
  593. Self::SocksError(err.to_string())
  594. }
  595. }
  596. #[cfg(feature = "wasm-runtime")]
  597. impl From<wasmer::CompileError> for Error {
  598. fn from(err: wasmer::CompileError) -> Self {
  599. Self::WasmerCompileError(err.to_string())
  600. }
  601. }
  602. #[cfg(feature = "wasm-runtime")]
  603. impl From<wasmer::ExportError> for Error {
  604. fn from(err: wasmer::ExportError) -> Self {
  605. Self::WasmerExportError(err.to_string())
  606. }
  607. }
  608. #[cfg(feature = "wasm-runtime")]
  609. impl From<wasmer::RuntimeError> for Error {
  610. fn from(err: wasmer::RuntimeError) -> Self {
  611. Self::WasmerRuntimeError(err.to_string())
  612. }
  613. }
  614. #[cfg(feature = "wasm-runtime")]
  615. impl From<wasmer::InstantiationError> for Error {
  616. fn from(err: wasmer::InstantiationError) -> Self {
  617. Self::WasmerInstantiationError(err.to_string())
  618. }
  619. }
  620. #[cfg(feature = "wasm-runtime")]
  621. impl From<wasmer::MemoryAccessError> for Error {
  622. fn from(err: wasmer::MemoryAccessError) -> Self {
  623. Self::WasmerMemoryError(err.to_string())
  624. }
  625. }
  626. #[cfg(feature = "wasm-runtime")]
  627. impl From<wasmer::MemoryError> for Error {
  628. fn from(err: wasmer::MemoryError) -> Self {
  629. Self::WasmerOomError(err.to_string())
  630. }
  631. }
  632. #[cfg(feature = "darkfi-sdk")]
  633. impl From<darkfi_sdk::error::ContractError> for Error {
  634. fn from(err: darkfi_sdk::error::ContractError) -> Self {
  635. Self::ContractError(err)
  636. }
  637. }