From 6b4007f6193ffa8a685a802eec7ff7be9431d112 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Wed, 14 Sep 2022 18:43:10 +0200 Subject: [PATCH] chore(etherscan): log response data on error (#1698) --- ethers-etherscan/src/lib.rs | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/ethers-etherscan/src/lib.rs b/ethers-etherscan/src/lib.rs index 5b150d5d..1f08e0b0 100644 --- a/ethers-etherscan/src/lib.rs +++ b/ethers-etherscan/src/lib.rs @@ -14,7 +14,7 @@ use std::{ path::PathBuf, time::{Duration, SystemTime, UNIX_EPOCH}, }; -use tracing::trace; +use tracing::{error, trace}; pub mod account; pub mod contract; pub mod errors; @@ -158,31 +158,41 @@ impl Client { form: &Form, ) -> Result> { trace!(target: "etherscan", "POST FORM {}", self.etherscan_api_url); - Ok(self + let response = self .client .post(self.etherscan_api_url.clone()) .header(header::CONTENT_TYPE, "application/x-www-form-urlencoded") .form(form) .send() .await? - .json() - .await?) + .text() + .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 async fn get_json(&self, query: &Q) -> Result> { trace!(target: "etherscan", "GET JSON {}", self.etherscan_api_url); - let res: ResponseData = self + let response = self .client .get(self.etherscan_api_url.clone()) .header(header::ACCEPT, "application/json") .query(query) .send() .await? - .json() + .text() .await?; - match res { + let response: ResponseData = serde_json::from_str(&response).map_err(|err| { + error!(target: "etherscan", ?response, "Failed to deserialize response: {}", err); + err + })?; + + match response { ResponseData::Error { result, .. } => { if result.starts_with("Max rate limit reached") { Err(EtherscanError::RateLimitExceeded)