error.rs 2.9 KB

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