From 3515bf748c24e231b333ce63b6fe1b36cd9bcc21 Mon Sep 17 00:00:00 2001 From: Ignotus Peverell Date: Sat, 6 Jan 2018 23:27:21 +0000 Subject: [PATCH] Improved receive error handling Better errors and messages returned by the push tx API. And better message in the command line receive. Fixes #585 --- api/src/client.rs | 18 +++++++++++++++--- api/src/handlers.rs | 11 +++++++---- src/bin/grin.rs | 10 +++++++++- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/api/src/client.rs b/api/src/client.rs index a1b283ce2..286f7f7fb 100644 --- a/api/src/client.rs +++ b/api/src/client.rs @@ -19,6 +19,7 @@ use hyper::client::Response; use hyper::status::{StatusClass, StatusCode}; use serde::{Deserialize, Serialize}; use serde_json; +use std::io::Read; use rest::Error; @@ -57,15 +58,26 @@ fn check_error(res: hyper::Result) -> Result { if let Err(e) = res { return Err(Error::Internal(format!("Error during request: {}", e))); } - let response = res.unwrap(); + let mut response = res.unwrap(); match response.status.class() { StatusClass::Success => Ok(response), - StatusClass::ServerError => Err(Error::Internal(format!("Server error."))), + StatusClass::ServerError => { + Err(Error::Internal(format!("Server error: {}", err_msg(&mut response)))) + } StatusClass::ClientError => if response.status == StatusCode::NotFound { Err(Error::NotFound) } else { - Err(Error::Argument(format!("Argument error"))) + Err(Error::Argument(format!("Argument error: {}", err_msg(&mut response)))) }, _ => Err(Error::Internal(format!("Unrecognized error."))), } } + +fn err_msg(resp: &mut Response) -> String { + let mut msg = String::new(); + if let Err(_) = resp.read_to_string(&mut msg) { + "".to_owned() + } else { + msg + } +} diff --git a/api/src/handlers.rs b/api/src/handlers.rs index d21ff2449..85f71a992 100644 --- a/api/src/handlers.rs +++ b/api/src/handlers.rs @@ -440,13 +440,16 @@ where tx.inputs.len(), tx.outputs.len() ); - self.tx_pool + + let res = self.tx_pool .write() .unwrap() - .add_to_memory_pool(source, tx) - .map_err(|e| Error::Internal(format!("Addition to transaction pool failed: {:?}", e)))?; + .add_to_memory_pool(source, tx); - Ok(Response::with(status::Ok)) + match res { + Ok(()) => Ok(Response::with(status::Ok)), + Err(e) => Err(IronError::from(Error::Argument(format!("{:?}", e)))) + } } } diff --git a/src/bin/grin.rs b/src/bin/grin.rs index 8e8729f25..8fd8c783d 100644 --- a/src/bin/grin.rs +++ b/src/bin/grin.rs @@ -435,7 +435,15 @@ fn wallet_command(wallet_args: &ArgMatches, global_config: GlobalConfig) { file.read_to_string(&mut contents).expect( "Unable to read transaction file.", ); - wallet::receive_json_tx_str(&wallet_config, &keychain, contents.as_str()).unwrap(); + if let Err(e) = + wallet::receive_json_tx_str( + &wallet_config, &keychain, contents.as_str()) { + + println!("Error receiving transaction, the most likely reasons are:"); + println!(" * the transaction has already been sent"); + println!(" * your node isn't running or can't be reached"); + println!("\nDetailed error: {:?}", e); + } } ("send", Some(send_args)) => { let amount = send_args.value_of("amount").expect(