chore(etherscan): log response data on error (#1698)

This commit is contained in:
Matthias Seitz 2022-09-14 18:43:10 +02:00 committed by GitHub
parent 62dd635a75
commit 6b4007f619
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 7 deletions

View File

@ -14,7 +14,7 @@ use std::{
path::PathBuf, path::PathBuf,
time::{Duration, SystemTime, UNIX_EPOCH}, time::{Duration, SystemTime, UNIX_EPOCH},
}; };
use tracing::trace; use tracing::{error, trace};
pub mod account; pub mod account;
pub mod contract; pub mod contract;
pub mod errors; pub mod errors;
@ -158,31 +158,41 @@ impl Client {
form: &Form, form: &Form,
) -> Result<Response<T>> { ) -> Result<Response<T>> {
trace!(target: "etherscan", "POST FORM {}", self.etherscan_api_url); trace!(target: "etherscan", "POST FORM {}", self.etherscan_api_url);
Ok(self let response = self
.client .client
.post(self.etherscan_api_url.clone()) .post(self.etherscan_api_url.clone())
.header(header::CONTENT_TYPE, "application/x-www-form-urlencoded") .header(header::CONTENT_TYPE, "application/x-www-form-urlencoded")
.form(form) .form(form)
.send() .send()
.await? .await?
.json() .text()
.await?) .await?;
Ok(serde_json::from_str(&response).map_err(|err| {
error!(target: "etherscan", ?response, "Failed to deserialize response: {}", err);
err
})?)
} }
/// Execute an API GET request with parameters /// Execute an API GET request with parameters
async fn get_json<T: DeserializeOwned, Q: Serialize>(&self, query: &Q) -> Result<Response<T>> { async fn get_json<T: DeserializeOwned, Q: Serialize>(&self, query: &Q) -> Result<Response<T>> {
trace!(target: "etherscan", "GET JSON {}", self.etherscan_api_url); trace!(target: "etherscan", "GET JSON {}", self.etherscan_api_url);
let res: ResponseData<T> = self let response = self
.client .client
.get(self.etherscan_api_url.clone()) .get(self.etherscan_api_url.clone())
.header(header::ACCEPT, "application/json") .header(header::ACCEPT, "application/json")
.query(query) .query(query)
.send() .send()
.await? .await?
.json() .text()
.await?; .await?;
match res { let response: ResponseData<T> = serde_json::from_str(&response).map_err(|err| {
error!(target: "etherscan", ?response, "Failed to deserialize response: {}", err);
err
})?;
match response {
ResponseData::Error { result, .. } => { ResponseData::Error { result, .. } => {
if result.starts_with("Max rate limit reached") { if result.starts_with("Max rate limit reached") {
Err(EtherscanError::RateLimitExceeded) Err(EtherscanError::RateLimitExceeded)