From 63f1742357226e3f7fca468610c4d2fe93f1c3b1 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Thu, 15 Sep 2022 21:44:52 +0200 Subject: [PATCH] feat(etherscan): add blocked by cloudflare error (#1703) --- ethers-etherscan/src/errors.rs | 9 +++++++++ ethers-etherscan/src/lib.rs | 18 ++++++++++++++---- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/ethers-etherscan/src/errors.rs b/ethers-etherscan/src/errors.rs index 060b518f..2ff4d0c6 100644 --- a/ethers-etherscan/src/errors.rs +++ b/ethers-etherscan/src/errors.rs @@ -37,4 +37,13 @@ pub enum EtherscanError { MissingSolcVersion(String), #[error("Invalid API Key")] InvalidApiKey, + #[error("Sorry, you have been blocked by Cloudflare, See also https://community.cloudflare.com/t/sorry-you-have-been-blocked/110790")] + BlockedByCloudflare, +} + +/// etherscan/polyscan is protected by cloudflare, which can lead to html responses like `Sorry, you have been blocked` See also +/// +/// This returns true if the `txt` is a cloudflare error response +pub(crate) fn is_blocked_by_cloudflare_response(txt: &str) -> bool { + txt.to_lowercase().contains("sorry, you have been blocked") } diff --git a/ethers-etherscan/src/lib.rs b/ethers-etherscan/src/lib.rs index 1f08e0b0..33fd37cf 100644 --- a/ethers-etherscan/src/lib.rs +++ b/ethers-etherscan/src/lib.rs @@ -1,5 +1,6 @@ //! Bindings for [etherscan.io web api](https://docs.etherscan.io/) +use crate::errors::is_blocked_by_cloudflare_response; use contract::ContractMetadata; use errors::EtherscanError; use ethers_core::{ @@ -15,6 +16,7 @@ use std::{ time::{Duration, SystemTime, UNIX_EPOCH}, }; use tracing::{error, trace}; + pub mod account; pub mod contract; pub mod errors; @@ -168,10 +170,14 @@ impl Client { .text() .await?; - Ok(serde_json::from_str(&response).map_err(|err| { + serde_json::from_str(&response).map_err(|err| { error!(target: "etherscan", ?response, "Failed to deserialize response: {}", err); - err - })?) + if is_blocked_by_cloudflare_response(&response) { + EtherscanError::BlockedByCloudflare + } else { + EtherscanError::Serde(err) + } + }) } /// Execute an API GET request with parameters @@ -189,7 +195,11 @@ impl Client { let response: ResponseData = serde_json::from_str(&response).map_err(|err| { error!(target: "etherscan", ?response, "Failed to deserialize response: {}", err); - err + if is_blocked_by_cloudflare_response(&response) { + EtherscanError::BlockedByCloudflare + } else { + EtherscanError::Serde(err) + } })?; match response {