error.rs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. use darkfi::rpc::jsonrpc::{ErrorCode::ServerError, JsonError, JsonResult};
  19. /// Custom RPC errors available for darkfid.
  20. /// Please sort them sensefully.
  21. pub enum RpcError {
  22. /*
  23. // Wallet/Key-related errors
  24. NoRowsFoundInWallet = -32101,
  25. Keygen = -32101,
  26. KeypairFetch = -32102,
  27. KeypairNotFound = -32103,
  28. InvalidKeypair = -32104,
  29. InvalidAddressParam = -32105,
  30. DecryptionFailed = -32106,
  31. */
  32. // Transaction-related errors
  33. TxSimulationFail = -32110,
  34. TxBroadcastFail = -32111,
  35. // State-related errors,
  36. NotSynced = -32120,
  37. UnknownSlot = -32121,
  38. // Parsing errors
  39. ParseError = -32190,
  40. // Contract-related errors
  41. ContractZkasDbNotFound = -32200,
  42. }
  43. fn to_tuple(e: RpcError) -> (i32, String) {
  44. let msg = match e {
  45. /*
  46. // Wallet/Key-related errors
  47. RpcError::NoRowsFoundInWallet => "No queried rows found in wallet",
  48. RpcError::Keygen => "Failed generating keypair",
  49. RpcError::KeypairFetch => "Failed fetching keypairs from wallet",
  50. RpcError::KeypairNotFound => "Keypair not found",
  51. RpcError::InvalidKeypair => "Invalid keypair",
  52. RpcError::InvalidAddressParam => "Invalid address parameter",
  53. RpcError::DecryptionFailed => "Decryption failed",
  54. */
  55. // Transaction-related errors
  56. RpcError::TxSimulationFail => "Failed simulating transaction state change",
  57. RpcError::TxBroadcastFail => "Failed broadcasting transaction",
  58. // State-related errors
  59. RpcError::NotSynced => "Blockchain is not synced",
  60. RpcError::UnknownSlot => "Did not find slot",
  61. // Parsing errors
  62. RpcError::ParseError => "Parse error",
  63. // Contract-related errors
  64. RpcError::ContractZkasDbNotFound => "zkas database not found for given contract",
  65. };
  66. (e as i32, msg.to_string())
  67. }
  68. pub fn server_error(e: RpcError, id: u16, msg: Option<&str>) -> JsonResult {
  69. let (code, default_msg) = to_tuple(e);
  70. if let Some(message) = msg {
  71. return JsonError::new(ServerError(code), Some(message.to_string()), id).into()
  72. }
  73. JsonError::new(ServerError(code), Some(default_msg), id).into()
  74. }