error.rs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. Keygen = -32101,
  25. KeypairFetch = -32102,
  26. KeypairNotFound = -32103,
  27. InvalidKeypair = -32104,
  28. InvalidAddressParam = -32105,
  29. DecryptionFailed = -32106,
  30. // Transaction-related errors
  31. TxBuildFail = -32110,
  32. TxBroadcastFail = -32111,
  33. TxSimulationFail = -32112,
  34. // State-related errors,
  35. NotSynced = -32120,
  36. UnknownSlot = -32121,
  37. // Parsing errors
  38. ParseError = -32190,
  39. NaN = -32191,
  40. LessThanNegOne = -32192,
  41. }
  42. fn to_tuple(e: RpcError) -> (i64, String) {
  43. let msg = match e {
  44. // Wallet/Key-related errors
  45. RpcError::Keygen => "Failed generating keypair",
  46. RpcError::KeypairFetch => "Failed fetching keypairs from wallet",
  47. RpcError::KeypairNotFound => "Keypair not found",
  48. RpcError::InvalidKeypair => "Invalid keypair",
  49. RpcError::InvalidAddressParam => "Invalid address parameter",
  50. RpcError::DecryptionFailed => "Decryption failed",
  51. // Transaction-related errors
  52. RpcError::TxBuildFail => "Failed building transaction",
  53. RpcError::TxBroadcastFail => "Failed broadcasting transaction",
  54. RpcError::TxSimulationFail => "Failed simulating transaction state change",
  55. // State-related errors
  56. RpcError::NotSynced => "Blockchain is not synced",
  57. RpcError::UnknownSlot => "Did not find slot",
  58. // Parsing errors
  59. RpcError::ParseError => "Parse error",
  60. RpcError::NaN => "Not a number",
  61. RpcError::LessThanNegOne => "Number cannot be lower than -1",
  62. };
  63. (e as i64, msg.to_string())
  64. }
  65. pub fn server_error(e: RpcError, id: Value, msg: Option<&str>) -> JsonResult {
  66. let (code, default_msg) = to_tuple(e);
  67. if let Some(message) = msg {
  68. return JsonError::new(ServerError(code), Some(message.to_string()), id).into()
  69. }
  70. JsonError::new(ServerError(code), Some(default_msg), id).into()
  71. }