error.rs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. #[error("Verify proof failed: '{0}', '{0}'")]
  18. VerifyProofFailed(usize, String),
  19. }
  20. pub type DaoResult<T> = std::result::Result<T, DaoError>;
  21. pub enum RpcError {
  22. Vote = -32101,
  23. Propose = -32102,
  24. Exec = -32103,
  25. Airdrop = -32104,
  26. Mint = -32105,
  27. Keygen = -32106,
  28. Create = -32107,
  29. Parse = -32108,
  30. Balance = -32109,
  31. }
  32. fn to_tuple(e: RpcError) -> (i64, String) {
  33. let msg = match e {
  34. RpcError::Vote => "Failed to cast a Vote",
  35. RpcError::Propose => "Failed to generate a Proposal",
  36. RpcError::Airdrop => "Failed to transfer an airdrop",
  37. RpcError::Keygen => "Failed to generate keypair",
  38. RpcError::Create => "Failed to create DAO",
  39. RpcError::Exec => "Failed to execute Proposal",
  40. RpcError::Mint => "Failed to mint DAO treasury",
  41. RpcError::Parse => "Generic parsing error",
  42. RpcError::Balance => "Failed to get balance",
  43. };
  44. (e as i64, msg.to_string())
  45. }
  46. pub fn server_error(e: RpcError, id: Value) -> JsonResult {
  47. let (code, msg) = to_tuple(e);
  48. JsonError::new(ServerError(code), Some(msg), id).into()
  49. }