Parcourir la source

Error handling

Dastan-glitch il y a 4 ans
Parent
commit
80ba9a2dca
5 fichiers modifiés avec 19 ajouts et 6 suppressions
  1. 1 0
      README.md
  2. 1 1
      src/client.rs
  3. 7 3
      src/rpc/jsonrpc.rs
  4. 9 1
      src/state.rs
  5. 1 1
      src/tx/mod.rs

+ 1 - 0
README.md

@@ -39,6 +39,7 @@ setup params, and compile the source code.
 
 ```shell
 % git clone https://github.com/darkrenaissance/darkfi
+% cd darkfi/
 % make
 ```
 

+ 1 - 1
src/client.rs

@@ -44,7 +44,7 @@ pub enum ClientFailed {
     DoesNotHaveKeypair,
     #[error("Password is empty. Cannot create database")]
     EmptyPassword,
-    #[error("Wallet already initalized")]
+    #[error("Wallet already initialized")]
     WalletInitialized,
     #[error("Keypair already exists")]
     KeyExists,

+ 7 - 3
src/rpc/jsonrpc.rs

@@ -143,9 +143,13 @@ pub async fn send_raw_request(url: &str, data: Value) -> Result<JsonResult, Erro
         }
     }
 
-    // TODO: Error handling
-    let host = parsed_url.host().unwrap().to_string();
-    let port = parsed_url.port().unwrap();
+    let host = parsed_url
+        .host()
+        .ok_or_else(|| Error::UrlParseError(format!("Missing host in {}", url)))?
+        .to_string();
+    let port = parsed_url
+        .port()
+        .ok_or_else(|| Error::UrlParseError(format!("Missing port in {}", url)))?;
 
     let socket_addr = {
         let host = host.clone();

+ 9 - 1
src/state.rs

@@ -14,7 +14,7 @@ use crate::{
     },
     tx::Transaction,
     wallet::walletdb::WalletPtr,
-    Result,
+    Result, error,
 };
 
 pub trait ProgramState {
@@ -53,6 +53,14 @@ pub enum VerifyFailed {
     MissingFunds,
     #[error("Assets don't match some inputs or outputs (token commits)")]
     AssetMismatch,
+    #[error("Inetrnal error: {0}")]
+    InternalError(String),
+}
+
+impl From<error::Error> for VerifyFailed {
+    fn from(err: error::Error) -> Self {
+        VerifyFailed::InternalError(err.to_string())
+    }
 }
 
 pub fn state_transition<S: ProgramState>(state: &S, tx: Transaction) -> VerifyResult<StateUpdate> {

+ 1 - 1
src/tx/mod.rs

@@ -125,7 +125,7 @@ impl Transaction {
 
         // Verify signatures
         let mut unsigned_tx_data = vec![];
-        self.encode_without_signature(&mut unsigned_tx_data).expect("TODO handle this");
+        self.encode_without_signature(&mut unsigned_tx_data)?;
         for (i, input) in self.clear_inputs.iter().enumerate() {
             let public = &input.signature_public;
             if !public.verify(&unsigned_tx_data[..], &input.signature) {