error.rs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. use serde_json::Value;
  2. use darkfi::rpc::jsonrpc::{ErrorCode::ServerError, JsonError, JsonResult};
  3. /// Custom RPC errors available for darkfid.
  4. /// Please sort them sensefully.
  5. pub enum RpcError {
  6. // Wallet/Key-related errors
  7. Keygen = -32101,
  8. KeypairFetch = -32102,
  9. KeypairNotFound = -32103,
  10. InvalidKeypair = -32104,
  11. InvalidAddressParam = -32105,
  12. DecryptionFailed = -32106,
  13. // Transaction-related errors
  14. TxBuildFail = -32110,
  15. TxBroadcastFail = -32111,
  16. TxSimulationFail = -32112,
  17. // State-related errors,
  18. NotSynced = -32120,
  19. UnknownSlot = -32121,
  20. // Parsing errors
  21. ParseError = -32190,
  22. NaN = -32191,
  23. LessThanNegOne = -32192,
  24. }
  25. fn to_tuple(e: RpcError) -> (i64, String) {
  26. let msg = match e {
  27. // Wallet/Key-related errors
  28. RpcError::Keygen => "Failed generating keypair",
  29. RpcError::KeypairFetch => "Failed fetching keypairs from wallet",
  30. RpcError::KeypairNotFound => "Keypair not found",
  31. RpcError::InvalidKeypair => "Invalid keypair",
  32. RpcError::InvalidAddressParam => "Invalid address parameter",
  33. RpcError::DecryptionFailed => "Decryption failed",
  34. // Transaction-related errors
  35. RpcError::TxBuildFail => "Failed building transaction",
  36. RpcError::TxBroadcastFail => "Failed broadcasting transaction",
  37. RpcError::TxSimulationFail => "Failed simulating transaction state change",
  38. // State-related errors
  39. RpcError::NotSynced => "Blockchain is not synced",
  40. RpcError::UnknownSlot => "Did not find slot",
  41. // Parsing errors
  42. RpcError::ParseError => "Parse error",
  43. RpcError::NaN => "Not a number",
  44. RpcError::LessThanNegOne => "Number cannot be lower than -1",
  45. };
  46. (e as i64, msg.to_string())
  47. }
  48. pub fn server_error(e: RpcError, id: Value, msg: Option<&str>) -> JsonResult {
  49. let (code, default_msg) = to_tuple(e);
  50. if let Some(message) = msg {
  51. return JsonError::new(ServerError(code), Some(message.to_string()), id).into()
  52. }
  53. JsonError::new(ServerError(code), Some(default_msg), id).into()
  54. }