error.rs 840 B

12345678910111213141516171819202122232425262728
  1. use serde_json::Value;
  2. use darkfi::rpc::jsonrpc::{ErrorCode::ServerError, JsonError, JsonResult};
  3. pub enum RpcError {
  4. UnknownKey = -35107,
  5. QueryFailed = -35108,
  6. KeyInsertFail = -35110,
  7. KeyRemoveFail = -35111,
  8. WaitingNetworkError = -35112,
  9. }
  10. fn to_tuple(e: RpcError) -> (i64, String) {
  11. let msg = match e {
  12. RpcError::UnknownKey => "Did not find key",
  13. RpcError::QueryFailed => "Failed to query key",
  14. RpcError::KeyInsertFail => "Failed to insert key",
  15. RpcError::KeyRemoveFail => "Failed to remove key",
  16. RpcError::WaitingNetworkError => "Error while waiting network response.",
  17. };
  18. (e as i64, msg.to_string())
  19. }
  20. pub fn server_error(e: RpcError, id: Value) -> JsonResult {
  21. let (code, msg) = to_tuple(e);
  22. JsonError::new(ServerError(code), Some(msg), id).into()
  23. }