Return errors back to RPC clients

and fix error handling in more places.
This commit is contained in:
Roman Zeyde 2018-05-25 23:05:25 +03:00
parent 2355dbd0ed
commit bf92ceeb16
No known key found for this signature in database
GPG Key ID: 87CAE5FA46917CBB
2 changed files with 10 additions and 4 deletions

View File

@ -145,6 +145,6 @@ pub fn main() {
let config = Config::from_args();
setup_logging(&config);
if let Err(e) = run_server(&config) {
error!("{}", e.display_chain().to_string());
error!("{}", e.display_chain());
}
}

View File

@ -149,7 +149,7 @@ impl Connection {
let tx = tx.as_str().chain_err(|| "non-string tx")?;
let tx = hex::decode(&tx).chain_err(|| "non-hex tx")?;
let tx: Transaction = deserialize(&tx).chain_err(|| "failed to parse tx")?;
let txid = self.query.broadcast(&tx).chain_err(|| "broadcast failed")?;
let txid = self.query.broadcast(&tx)?;
Ok(json!(txid.be_hex_string()))
}
@ -196,9 +196,15 @@ impl Connection {
"blockchain.transaction.get" => self.blockchain_transaction_get(&params),
"blockchain.transaction.get_merkle" => self.blockchain_transaction_get_merkle(&params),
&_ => bail!("unknown method {} {:?}", method, params),
}?;
};
// TODO: return application errors should be sent to the client
Ok(json!({"jsonrpc": "2.0", "id": id, "result": result}))
Ok(match result {
Ok(result) => json!({"jsonrpc": "2.0", "id": id, "result": result}),
Err(e) => {
warn!("rpc #{} {} {:?} failed: {}", id, method, params, e.display_chain());
json!({"jsonrpc": "2.0", "id": id, "error": format!("{}", e)})
}
})
}
fn update_subscriptions(&mut self) -> Result<Vec<Value>> {