error.rs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. use serde_json::Value;
  2. use darkfi::rpc::jsonrpc::{ErrorCode::ServerError, JsonError, JsonResult};
  3. #[derive(Debug, thiserror::Error)]
  4. pub enum DaoError {
  5. #[error("No Proposals found")]
  6. NoProposals,
  7. #[error("No DAO params found")]
  8. DaoNotConfigured,
  9. #[error("State transition failed: '{0}'")]
  10. StateTransitionFailed(String),
  11. #[error("Wallet does not exist")]
  12. NoWalletFound,
  13. #[error("State not found")]
  14. StateNotFound,
  15. #[error("InternalError")]
  16. Darkfi(#[from] darkfi::error::Error),
  17. }
  18. pub type DaoResult<T> = std::result::Result<T, DaoError>;
  19. pub enum RpcError {
  20. Vote = -32101,
  21. Propose = -32102,
  22. Exec = -32103,
  23. Airdrop = -32104,
  24. Mint = -32105,
  25. Keygen = -32106,
  26. Create = -32107,
  27. Parse = -32108,
  28. Balance = -32109,
  29. }
  30. fn to_tuple(e: RpcError) -> (i64, String) {
  31. let msg = match e {
  32. RpcError::Vote => "Failed to cast a Vote",
  33. RpcError::Propose => "Failed to generate a Proposal",
  34. RpcError::Airdrop => "Failed to transfer an airdrop",
  35. RpcError::Keygen => "Failed to generate keypair",
  36. RpcError::Create => "Failed to create DAO",
  37. RpcError::Exec => "Failed to execute Proposal",
  38. RpcError::Mint => "Failed to mint DAO treasury",
  39. RpcError::Parse => "Generic parsing error",
  40. RpcError::Balance => "Failed to get balance",
  41. };
  42. (e as i64, msg.to_string())
  43. }
  44. pub fn server_error(e: RpcError, id: Value) -> JsonResult {
  45. let (code, msg) = to_tuple(e);
  46. JsonError::new(ServerError(code), Some(msg), id).into()
  47. }