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),
#[error("Local networks (e.g. anvil, ganache, geth --dev) cannot be indexed by etherscan")]
LocalNetworksNotSupported,
#[error("Received error response: status={status},message={message}, result={result:?}")]
ErrorResponse { status: String, message: String, result: Option<String> },
#[error("Unknown error: {0}")]
Unknown(String),
#[error("Missing field: {0}")]

View File

@ -214,14 +214,15 @@ impl Client {
})?;
match res {
ResponseData::Error { result, .. } => {
if result.starts_with("Max rate limit reached") {
Err(EtherscanError::RateLimitExceeded)
} else if result.to_lowercase() == "invalid api key" {
Err(EtherscanError::InvalidApiKey)
} else {
Err(EtherscanError::Unknown(result))
ResponseData::Error { result, message, status } => {
if let Some(ref result) = result {
if result.starts_with("Max rate limit reached") {
return Err(EtherscanError::RateLimitExceeded)
} else if result.to_lowercase() == "invalid api key" {
return Err(EtherscanError::InvalidApiKey)
}
}
Err(EtherscanError::ErrorResponse { status, message, result })
}
ResponseData::Success(res) => Ok(res),
}
@ -426,7 +427,7 @@ pub struct Response<T> {
#[serde(untagged)]
pub enum ResponseData<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
@ -461,9 +462,17 @@ fn into_url(url: impl IntoUrl) -> std::result::Result<Url, reqwest::Error> {
#[cfg(test)]
mod tests {
use crate::{Client, EtherscanError};
use crate::{Client, EtherscanError, ResponseData};
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]
fn test_api_paths() {
let client = Client::new(Chain::Goerli, "").unwrap();