Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle invalid api key response #56

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions crates/block-explorers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,17 +241,20 @@ impl Client {
})?;

match res {
ResponseData::Error { result, message, status } => {
ResponseData::Error { message, result } => {
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" {
}
if result.to_lowercase().contains("invalid api key") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please submit this change separately?

Copy link
Author

@JonoPrest JonoPrest Jul 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @mattsse, I'd be happy to but the reality is that it is a dead case without the change to the deserializer. The test case that I added and the original case where result == "invalid api key" can never be hit since it would deserialize as a ResponseData::Success case rathere than ResponseData::Error

Let me know if you would still like it as a separate change?

return Err(EtherscanError::InvalidApiKey);
}
}
Err(EtherscanError::ErrorResponse { status, message, result })
Err(EtherscanError::ErrorResponse { status: "0".to_string(), message, result })
}
ResponseData::Success { message, result } => {
Ok(Response { status: "1".to_string(), message, result })
}
ResponseData::Success(res) => Ok(res),
}
}

Expand Down Expand Up @@ -495,11 +498,13 @@ pub struct Response<T> {
pub result: T,
}

#[derive(Deserialize, Debug, Clone)]
#[serde(untagged)]
#[derive(Debug, Clone, Deserialize)]
#[serde(tag = "status")]
pub enum ResponseData<T> {
Success(Response<T>),
Error { status: String, message: String, result: Option<String> },
#[serde(rename = "1")]
Success { message: String, result: T },
#[serde(rename = "0")]
Error { message: String, result: Option<String> },
}

/// The type that gets serialized as query
Expand Down Expand Up @@ -527,6 +532,7 @@ mod tests {
use crate::{Client, EtherscanError, ResponseData};
use alloy_chains::Chain;
use alloy_primitives::{Address, B256};
use serde_json::json;

// <https://github.com/foundry-rs/foundry/issues/4406>
#[test]
Expand All @@ -536,6 +542,17 @@ mod tests {
assert!(matches!(resp, ResponseData::Error { .. }));
}

#[test]
fn can_parse_etherscan_mainnet_invalid_api_key() {
let err = json!({
"status":"0",
"message":"NOTOK",
"result":"Missing/Invalid API Key"
});
let resp: ResponseData<String> = serde_json::from_value(err).unwrap();
assert!(matches!(resp, ResponseData::Error { .. }));
}

#[test]
fn test_api_paths() {
let client = Client::new(Chain::goerli(), "").unwrap();
Expand Down