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

massively improved jsonrpc_core error handling. implemented emptypassword error

lunar-mining 5 лет назад
Родитель
Сommit
c262bee011
2 измененных файлов с 14 добавлено и 16 удалено
  1. 5 2
      src/error.rs
  2. 9 14
      src/wallet/walletdb.rs

+ 5 - 2
src/error.rs

@@ -47,6 +47,7 @@ pub enum Error {
     TreeFull,
     SerdeJsonError(String),
     SurfHttpError(String),
+    EmptyPassword,
 }
 
 impl std::error::Error for Error {}
@@ -90,6 +91,7 @@ impl fmt::Display for Error {
             Error::TreeFull => f.write_str("MerkleTree is full"),
             Error::SerdeJsonError(ref err) => write!(f, "Json serialization error: {}", err),
             Error::SurfHttpError(ref err) => write!(f, "Surf Http error: {}", err),
+            Error::EmptyPassword => f.write_str("Password is empty. Cannot create database"),
         }
     }
 }
@@ -113,9 +115,10 @@ impl From<jsonrpc_core::Error> for Error {
     }
 }
 
+// err.fmt();
 impl From<Error> for jsonrpc_core::Error {
-    fn from(_err: Error) -> jsonrpc_core::Error {
-        jsonrpc_core::Error::parse_error()
+    fn from(err: Error) -> jsonrpc_core::Error {
+        jsonrpc_core::Error::invalid_params(err.to_string())
     }
 }
 

+ 9 - 14
src/wallet/walletdb.rs

@@ -2,7 +2,7 @@ use crate::crypto::{coin::Coin, merkle::IncrementalWitness, merkle_node::MerkleN
 use crate::serial;
 use crate::serial::{deserialize, serialize, Decodable, Encodable};
 use crate::util::join_config_path;
-use crate::Result;
+use crate::{Result, Error};
 
 use async_std::sync::{Arc, Mutex};
 use ff::Field;
@@ -52,22 +52,20 @@ impl WalletDB {
     }
 
     pub fn init_db(&self) -> Result<()> {
-        let conn = Connection::open(&self.path)?;
-        debug!(target: "walletdb", "OPENED CONNECTION AT PATH {:?}", self.path);
-        let contents = include_str!("../../res/schema.sql");
         if !self.password.trim().is_empty() {
+            let contents = include_str!("../../res/schema.sql");
+            let conn = Connection::open(&self.path)?;
+            debug!(target: "walletdb", "OPENED CONNECTION AT PATH {:?}", self.path);
             conn.execute(
                 "PRAGMA key=(?1)",
                 params![self.password],
             )?;
-            match conn.execute_batch(&contents) {
-                Ok(v) => println!("Database initalized successfully {:?}", v),
-                Err(err) => println!("Error: {}", err),
-            };
+            conn.execute_batch(&contents)?
         }
         else {
-            println!("Password is empty. You must set a password to use the wallet.")
-        };
+            println!("Password is empty. You must set a password to use the wallet.");
+            return Err(Error::EmptyPassword);
+        }
         Ok(())
     }
 
@@ -75,10 +73,7 @@ impl WalletDB {
         let conn = Connection::open(&self.path)?;
         debug!(target: "walletdb", "OPENED CONNECTION AT PATH {:?}", self.path);
         let contents = include_str!("../../res/schema.sql");
-        match conn.execute_batch(&contents) {
-            Ok(v) => println!("Database initalized successfully {:?}", v),
-            Err(err) => println!("Error: {}", err),
-        };
+        conn.execute_batch(&contents)?;
         Ok(())
     }