瀏覽代碼

print the error msg when reply has an error

ghassmo 5 年之前
父節點
當前提交
d2205c184b
共有 2 個文件被更改,包括 22 次插入6 次删除
  1. 18 3
      src/service/gateway.rs
  2. 4 3
      src/service/reqrep.rs

+ 18 - 3
src/service/gateway.rs

@@ -17,6 +17,8 @@ enum GatewayError {
     IndexNotExist,
 }
 
+
+
 #[repr(u8)]
 enum GatewayCommand {
     PutSlab,
@@ -229,7 +231,7 @@ impl GatewayClient {
     pub async fn get_slab(&mut self, index: u64) -> Result<Option<Slab>> {
         let rep = self
             .protocol
-            .request(GatewayCommand::GetSlab as u8, serialize(&index))
+            .request(GatewayCommand::GetSlab as u8, serialize(&index), &handle_error)
             .await?;
 
         if let Some(slab) = rep {
@@ -249,7 +251,7 @@ impl GatewayClient {
 
             let rep = self
                 .protocol
-                .request(GatewayCommand::PutSlab as u8, slab.clone())
+                .request(GatewayCommand::PutSlab as u8, slab.clone(), &handle_error)
                 .await?;
 
             if let Some(_) = rep {
@@ -262,7 +264,7 @@ impl GatewayClient {
     pub async fn get_last_index(&mut self) -> Result<u64> {
         let rep = self
             .protocol
-            .request(GatewayCommand::GetLastIndex as u8, vec![])
+            .request(GatewayCommand::GetLastIndex as u8, vec![], &handle_error)
             .await?;
         if let Some(index) = rep {
             return Ok(deserialize(&index)?);
@@ -303,3 +305,16 @@ impl GatewayClient {
         }
     }
 }
+
+
+fn handle_error(status_code: u32) {
+    match status_code {
+        1 => {
+            warn!("Reply has an Error: Index is not updated");
+        }
+        2 => {
+            warn!("Reply has an Error: Index Not Exist");
+        }
+        _ => {}
+    }
+}

+ 4 - 3
src/service/reqrep.rs

@@ -144,7 +144,7 @@ impl ReqProtocol {
         Ok(())
     }
 
-    pub async fn request(&mut self, command: u8, data: Vec<u8>) -> Result<Option<Vec<u8>>> {
+    pub async fn request(&mut self, command: u8, data: Vec<u8>, handle_error: &dyn Fn(u32)) -> Result<Option<Vec<u8>>> {
         let request = Request::new(command, data);
         let req = serialize(&request);
         let req = bytes::Bytes::from(req);
@@ -168,9 +168,10 @@ impl ReqProtocol {
                 reply.has_error()
             );
 
-            // TODO return error status code instead of None
             if reply.has_error() {
-                warn!("Reply has an error {}", reply.get_error());
+                // TODO return error status code instead of None
+                // this is temporary
+                handle_error(reply.get_error());
                 return Ok(None);
             }