Просмотр исходного кода

darkfid: Clean up RPC error messages and handling.

parazyd 4 лет назад
Родитель
Сommit
b3f5ad1c55
2 измененных файлов с 32 добавлено и 44 удалено
  1. 21 33
      bin/darkfid2/src/error.rs
  2. 11 11
      bin/darkfid2/src/main.rs

+ 21 - 33
bin/darkfid2/src/error.rs

@@ -5,41 +5,29 @@ use darkfi::rpc::{
     jsonrpc::{ErrorCode::ServerError, JsonResult},
 };
 
-const ERROR_KEYGEN: i64 = -32101;
-const ERROR_NAN: i64 = -32102;
-const ERROR_LT1: i64 = -32103;
-const ERROR_KP_FETCH: i64 = -32104;
-const ERROR_KP_NOT_FOUND: i64 = -32105;
-const ERROR_INVALID_KP: i64 = -32106;
-
-pub fn err_keygen(id: Value) -> JsonResult {
-    jsonrpc::error(ServerError(ERROR_KEYGEN), Some("Failed generating keypair".to_string()), id)
-        .into()
-}
-
-pub fn err_nan(id: Value) -> JsonResult {
-    jsonrpc::error(ServerError(ERROR_NAN), Some("Not a number".to_string()), id).into()
-}
-
-pub fn err_lt1(id: Value) -> JsonResult {
-    jsonrpc::error(ServerError(ERROR_LT1), Some("Number cannot be lower than -1".to_string()), id)
-        .into()
-}
-
-pub fn err_kp_fetch(id: Value) -> JsonResult {
-    jsonrpc::error(
-        ServerError(ERROR_KP_FETCH),
-        Some("Failed fetching keypairs from wallet".to_string()),
-        id,
-    )
-    .into()
+pub enum RpcError {
+    Keygen = -32101,
+    Nan = -32102,
+    LessThanNegOne = -32103,
+    KeypairFetch = -32104,
+    KeypairNotFound = -32105,
+    InvalidKeypair = -32106,
 }
 
-pub fn err_kp_not_found(id: Value) -> JsonResult {
-    jsonrpc::error(ServerError(ERROR_KP_NOT_FOUND), Some("Keypair not found".to_string()), id)
-        .into()
+fn to_tuple(e: RpcError) -> (i64, String) {
+    let msg = match e {
+        RpcError::Keygen => "Failed generating keypair",
+        RpcError::Nan => "Not a number",
+        RpcError::LessThanNegOne => "Number cannot be lower than -1",
+        RpcError::KeypairFetch => "Failed fetching keypairs from wallet",
+        RpcError::KeypairNotFound => "Keypair not found",
+        RpcError::InvalidKeypair => "Invalid keypair",
+    };
+
+    (e as i64, msg.to_string())
 }
 
-pub fn err_invalid_kp(id: Value) -> JsonResult {
-    jsonrpc::error(ServerError(ERROR_INVALID_KP), Some("Invalid keypair".to_string()), id).into()
+pub fn server_error(e: RpcError, id: Value) -> JsonResult {
+    let (code, msg) = to_tuple(e);
+    jsonrpc::error(ServerError(code), Some(msg), id).into()
 }

+ 11 - 11
bin/darkfid2/src/main.rs

@@ -38,7 +38,7 @@ mod client;
 use client::Client;
 
 mod error;
-use error::*;
+use error::{server_error, RpcError};
 
 const CONFIG_FILE: &str = "darkfid_config.toml";
 const CONFIG_FILE_CONTENTS: &str = include_str!("../darkfid_config.toml");
@@ -117,7 +117,7 @@ impl Darkfid {
             Ok(a) => jsonrpc::response(json!(a.to_string()), id).into(),
             Err(e) => {
                 error!("Failed creating keypair: {}", e);
-                err_keygen(id)
+                server_error(RpcError::Keygen, id)
             }
         }
     }
@@ -135,7 +135,7 @@ impl Darkfid {
         let mut fetch_all = false;
         for i in params {
             if !i.is_i64() {
-                return err_nan(id)
+                return server_error(RpcError::Nan, id)
             }
 
             if i.as_i64() == Some(-1) {
@@ -144,7 +144,7 @@ impl Darkfid {
             }
 
             if i.as_i64() < Some(-1) {
-                return err_lt1(id)
+                return server_error(RpcError::LessThanNegOne, id)
             }
         }
 
@@ -152,7 +152,7 @@ impl Darkfid {
             Ok(v) => v,
             Err(e) => {
                 error!("Failed fetching keypairs: {}", e);
-                return err_kp_fetch(id)
+                return server_error(RpcError::KeypairFetch, id)
             }
         };
 
@@ -190,7 +190,7 @@ impl Darkfid {
             Ok(v) => v,
             Err(e) => {
                 error!("Failed fetching keypairs: {}", e);
-                return err_kp_fetch(id)
+                return server_error(RpcError::KeypairFetch, id)
             }
         };
 
@@ -198,7 +198,7 @@ impl Darkfid {
             return jsonrpc::response(json!(kp.secret.to_bytes()), id).into()
         }
 
-        err_kp_not_found(id)
+        server_error(RpcError::KeypairNotFound, id)
     }
 
     // RPCAPI:
@@ -215,7 +215,7 @@ impl Darkfid {
             Ok(v) => v,
             Err(e) => {
                 error!("Failed parsing secret key from string: {}", e);
-                return err_invalid_kp(id)
+                return server_error(RpcError::InvalidKeypair, id)
             }
         };
 
@@ -223,7 +223,7 @@ impl Darkfid {
             Ok(v) => v,
             Err(e) => {
                 error!("Failed parsing secret key from string: {}", e);
-                return err_invalid_kp(id)
+                return server_error(RpcError::InvalidKeypair, id)
             }
         };
 
@@ -258,12 +258,12 @@ impl Darkfid {
             Ok(v) => v,
             Err(e) => {
                 error!("Failed fetching keypairs: {}", e);
-                return err_kp_fetch(id)
+                return server_error(RpcError::KeypairFetch, id)
             }
         };
 
         if keypairs.len() as u64 != idx - 1 {
-            return err_kp_not_found(id)
+            return server_error(RpcError::KeypairNotFound, id)
         }
 
         let kp = keypairs[idx as usize];