Handle errors in eth1 http module

This commit is contained in:
Paul Hauner
2019-11-26 14:42:41 +11:00
parent 743029a1de
commit f2374b3bc2

View File

@@ -138,7 +138,7 @@ pub fn get_deposit_count(
timeout, timeout,
) )
.and_then(|result| match result { .and_then(|result| match result {
None => Ok(None), None => Err(format!("Deposit root response was none")),
Some(bytes) => { Some(bytes) => {
if bytes.is_empty() { if bytes.is_empty() {
Ok(None) Ok(None)
@@ -175,7 +175,7 @@ pub fn get_deposit_root(
timeout, timeout,
) )
.and_then(|result| match result { .and_then(|result| match result {
None => Ok(None), None => Err(format!("Deposit root response was none")),
Some(bytes) => { Some(bytes) => {
if bytes.is_empty() { if bytes.is_empty() {
Ok(None) Ok(None)
@@ -373,13 +373,19 @@ pub fn send_rpc_request(
/// Accepts an entire HTTP body (as a string) and returns the `result` field, as a serde `Value`. /// Accepts an entire HTTP body (as a string) and returns the `result` field, as a serde `Value`.
fn response_result(response: &str) -> Result<Option<Value>, String> { fn response_result(response: &str) -> Result<Option<Value>, String> {
Ok(serde_json::from_str::<Value>(&response) let json = serde_json::from_str::<Value>(&response)
.map_err(|e| format!("Failed to parse response: {:?}", e))? .map_err(|e| format!("Failed to parse response: {:?}", e))?;
if let Some(error) = json.get("error") {
Err(format!("Eth1 node returned error: {}", error))
} else {
Ok(json
.get("result") .get("result")
.cloned() .cloned()
.map(Some) .map(Some)
.unwrap_or_else(|| None)) .unwrap_or_else(|| None))
} }
}
/// Parses a `0x`-prefixed, **big-endian** hex string as a u64. /// Parses a `0x`-prefixed, **big-endian** hex string as a u64.
/// ///