fix(etherscan): support null result (#2249)

This commit is contained in:
Matthias Seitz 2023-03-13 20:43:17 +01:00 committed by GitHub
parent 7ae2ed12bf
commit 862d923f2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 9 deletions

View File

@ -29,6 +29,8 @@ pub enum EtherscanError {
IO(#[from] std::io::Error), IO(#[from] std::io::Error),
#[error("Local networks (e.g. anvil, ganache, geth --dev) cannot be indexed by etherscan")] #[error("Local networks (e.g. anvil, ganache, geth --dev) cannot be indexed by etherscan")]
LocalNetworksNotSupported, LocalNetworksNotSupported,
#[error("Received error response: status={status},message={message}, result={result:?}")]
ErrorResponse { status: String, message: String, result: Option<String> },
#[error("Unknown error: {0}")] #[error("Unknown error: {0}")]
Unknown(String), Unknown(String),
#[error("Missing field: {0}")] #[error("Missing field: {0}")]

View File

@ -214,14 +214,15 @@ impl Client {
})?; })?;
match res { match res {
ResponseData::Error { result, .. } => { ResponseData::Error { result, message, status } => {
if result.starts_with("Max rate limit reached") { if let Some(ref result) = result {
Err(EtherscanError::RateLimitExceeded) if result.starts_with("Max rate limit reached") {
} else if result.to_lowercase() == "invalid api key" { return Err(EtherscanError::RateLimitExceeded)
Err(EtherscanError::InvalidApiKey) } else if result.to_lowercase() == "invalid api key" {
} else { return Err(EtherscanError::InvalidApiKey)
Err(EtherscanError::Unknown(result)) }
} }
Err(EtherscanError::ErrorResponse { status, message, result })
} }
ResponseData::Success(res) => Ok(res), ResponseData::Success(res) => Ok(res),
} }
@ -426,7 +427,7 @@ pub struct Response<T> {
#[serde(untagged)] #[serde(untagged)]
pub enum ResponseData<T> { pub enum ResponseData<T> {
Success(Response<T>), Success(Response<T>),
Error { status: String, message: String, result: String }, Error { status: String, message: String, result: Option<String> },
} }
/// The type that gets serialized as query /// The type that gets serialized as query
@ -461,9 +462,17 @@ fn into_url(url: impl IntoUrl) -> std::result::Result<Url, reqwest::Error> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::{Client, EtherscanError}; use crate::{Client, EtherscanError, ResponseData};
use ethers_core::types::{Address, Chain, H256}; use ethers_core::types::{Address, Chain, H256};
// <https://github.com/foundry-rs/foundry/issues/4406>
#[test]
fn can_parse_block_scout_err() {
let err = "{\"message\":\"Something went wrong.\",\"result\":null,\"status\":\"0\"}";
let resp: ResponseData<Address> = serde_json::from_str(err).unwrap();
assert!(matches!(resp, ResponseData::Error { .. }));
}
#[test] #[test]
fn test_api_paths() { fn test_api_paths() {
let client = Client::new(Chain::Goerli, "").unwrap(); let client = Client::new(Chain::Goerli, "").unwrap();