Skip to content

Commit

Permalink
Merge #113: Disallow empty blocks response
Browse files Browse the repository at this point in the history
340cd1c fix(client): disallow empty blocks response (Rob N)

Pull request description:

  This is my first attempt at resolving the empty blocks response at the client level. I didn't find that any of the current error variants accurately described the problem, but could use some outside opinions

  @oleonardolima @notmandatory

ACKs for top commit:
  ValuedMammal:
    ACK 340cd1c
  oleonardolima:
    utACK 340cd1c

Tree-SHA512: fc79b40fc499727d0061142d6906ae3b1f1672c507181c0c86d9527afdcdaea82e8b0e8af64a7e2463e3f4ed84595d666e854fe97d070a900cfe5a10d25b4af8
  • Loading branch information
ValuedMammal committed Dec 4, 2024
2 parents 9f4b3b9 + 340cd1c commit fa5b611
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,11 @@ impl<S: Sleeper> AsyncClient<S> {
Some(height) => format!("/blocks/{height}"),
None => "/blocks".to_string(),
};
self.get_response_json(&path).await
let blocks: Vec<BlockSummary> = self.get_response_json(&path).await?;
if blocks.is_empty() {
return Err(Error::InvalidResponse);
}
Ok(blocks)
}

/// Get the underlying base URL.
Expand Down
6 changes: 5 additions & 1 deletion src/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,11 @@ impl BlockingClient {
Some(height) => format!("/blocks/{}", height),
None => "/blocks".to_string(),
};
self.get_response_json(&path)
let blocks: Vec<BlockSummary> = self.get_response_json(&path)?;
if blocks.is_empty() {
return Err(Error::InvalidResponse);
}
Ok(blocks)
}

/// Sends a GET request to the given `url`, retrying failed attempts
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ pub enum Error {
InvalidHttpHeaderName(String),
/// Invalid HTTP Header value specified
InvalidHttpHeaderValue(String),
/// The server sent an invalid response
InvalidResponse,
}

impl fmt::Display for Error {
Expand Down

0 comments on commit fa5b611

Please sign in to comment.