Improved receive error handling

Better errors and messages returned by the push tx API. And better
message in the command line receive. Fixes #585
This commit is contained in:
Ignotus Peverell 2018-01-06 23:27:21 +00:00
parent abcecd82c1
commit 3515bf748c
No known key found for this signature in database
GPG key ID: 99CD25F39F8F8211
3 changed files with 31 additions and 8 deletions

View file

@ -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<Response>) -> Result<Response, Error> {
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) {
"<no message>".to_owned()
} else {
msg
}
}

View file

@ -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))))
}
}
}

View file

@ -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(