فهرست منبع

implemented custom error handling for jsonrpc_core

rachel-rose 5 سال پیش
والد
کامیت
bc93d1734e
2فایلهای تغییر یافته به همراه21 افزوده شده و 20 حذف شده
  1. 14 5
      src/error.rs
  2. 7 15
      src/rpc/jsonserver.rs

+ 14 - 5
src/error.rs

@@ -45,6 +45,7 @@ pub enum Error {
     JsonRpcError(String),
     RocksdbError(String),
     TreeFull,
+    SerdeJsonError(String),
 }
 
 impl std::error::Error for Error {}
@@ -86,6 +87,7 @@ impl fmt::Display for Error {
             Error::RocksdbError(ref err) => write!(f, "Rocksdb Error: {}", err),
             Error::JsonRpcError(ref err) => write!(f, "JsonRpc Error: {}", err),
             Error::TreeFull => f.write_str("MerkleTree is full"),
+            Error::SerdeJsonError(ref err) => write!(f, "Json serialization error: {}", err),
         }
     }
 }
@@ -110,11 +112,17 @@ impl From<jsonrpc_core::Error> for Error {
 }
 
 
-//impl From<Error> for jsonrpc_core::types::error::Error {
-//    fn from(err: Error) -> jsonrpc_core::types::error::Error {
-//        jsonrpc_core::types::error::Error::ErrorCode
-//    }
-//}
+impl From<Error> for jsonrpc_core::Error {
+    fn from(_err: Error) -> jsonrpc_core::Error {
+        jsonrpc_core::Error::parse_error()
+    }
+}
+
+impl From<serde_json::Error> for Error {
+    fn from(err: serde_json::Error) -> Error {
+        Error::SerdeJsonError(err.to_string())
+    }
+}
 
 impl From<std::io::Error> for Error {
     fn from(err: std::io::Error) -> Error {
@@ -175,3 +183,4 @@ impl From<state::VerifyFailed> for Error {
         Error::VerifyFailed
     }
 }
+

+ 7 - 15
src/rpc/jsonserver.rs

@@ -145,7 +145,7 @@ impl RpcInterface {
         io.add_method("get_key", move |_| {
             let self2 = self1.clone();
             async move {
-                self2.adapter.get_key().await.expect("Failed to get key");
+                self2.adapter.get_key().await?;
                 Ok(jsonrpc_core::Value::String("Getting cashier key...".into()))
             }
         });
@@ -156,8 +156,7 @@ impl RpcInterface {
                 self2
                     .adapter
                     .get_cash_key()
-                    .await
-                    .expect("Failed to get key");
+                    .await?;
                 Ok(jsonrpc_core::Value::String("Getting cashier key...".into()))
             }
         });
@@ -190,8 +189,7 @@ impl RpcInterface {
                 self2
                     .adapter
                     .init_db()
-                    .await
-                    .expect("Wallet generation failed");
+                    .await?;
                 Ok(jsonrpc_core::Value::String("Created wallet".into()))
             }
         });
@@ -203,8 +201,7 @@ impl RpcInterface {
                 self2
                     .adapter
                     .key_gen()
-                    .await
-                    .expect("Failed to generate key");
+                    .await?;
                 Ok(jsonrpc_core::Value::String(
                     "Attempted key generation".into(),
                 ))
@@ -218,8 +215,7 @@ impl RpcInterface {
                 self2
                     .adapter
                     .cash_key_gen()
-                    .await
-                    .expect("Failed to generate key");
+                    .await?;
                 Ok(jsonrpc_core::Value::String(
                     "Attempted key generation".into(),
                 ))
@@ -230,11 +226,7 @@ impl RpcInterface {
             let self2 = self1.clone();
             async move {
                 println!("Test wallet method called...");
-                // use map err to convert from own error to jsonrpc
-                // convert our error to string 
-                // use json to process error string
-
-                self2.adapter.test_wallet().await.expect("Wallet test failed");
+                self2.adapter.test_wallet().await?;
                 Ok(jsonrpc_core::Value::String("Test wallet".into()))
             }
         });
@@ -246,7 +238,7 @@ impl RpcInterface {
                 self2
                     .adapter
                     .init_cashier_db()
-                    .await.expect("Create wallet failed");
+                    .await?;
                 println!("Wallet created at path {:?}", self2.adapter.wallet.path);
                 Ok(jsonrpc_core::Value::String("Created cashier wallet".into()))
             }