Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
fix: handle provider error correctly (#1630)
Browse files Browse the repository at this point in the history
* fix: handle provider error correctly

* chore(clippy): make clippy happy

* chore: rustfmt

* convert http error
  • Loading branch information
mattsse authored Aug 22, 2022
1 parent 7c26550 commit 10f74f8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 25 deletions.
5 changes: 4 additions & 1 deletion ethers-providers/src/transports/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ pub enum ClientError {

impl From<ClientError> for ProviderError {
fn from(src: ClientError) -> Self {
ProviderError::JsonRpcClientError(Box::new(src))
match src {
ClientError::ReqwestError(err) => ProviderError::HTTPError(err),
_ => ProviderError::JsonRpcClientError(Box::new(src)),
}
}
}

Expand Down
39 changes: 15 additions & 24 deletions ethers-providers/src/transports/retry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::{provider::ProviderError, JsonRpcClient};
use async_trait::async_trait;
use serde::{de::DeserializeOwned, Serialize};
use std::{
error::Error,
fmt::Debug,
sync::atomic::{AtomicU32, Ordering},
time::Duration,
Expand Down Expand Up @@ -198,35 +197,27 @@ impl Default for RetryClientBuilder {
/// 2. Params serialization failed.
/// 3. Request timed out i.e. max retries were already made.
#[derive(Error, Debug)]
pub enum RetryClientError<T>
where
T: JsonRpcClient,
T::Error: Sync + Send + 'static,
{
pub enum RetryClientError {
#[error(transparent)]
ProviderError(T::Error),
ProviderError(ProviderError),
TimeoutError,
#[error(transparent)]
SerdeJson(serde_json::Error),
}

impl<T> std::fmt::Display for RetryClientError<T>
where
T: JsonRpcClient,
<T as JsonRpcClient>::Error: Sync + Send + 'static,
{
impl std::fmt::Display for RetryClientError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self)
}
}

impl<T> From<RetryClientError<T>> for ProviderError
where
T: JsonRpcClient + 'static,
<T as JsonRpcClient>::Error: Sync + Send + 'static,
{
fn from(src: RetryClientError<T>) -> Self {
ProviderError::JsonRpcClientError(Box::new(src))
impl From<RetryClientError> for ProviderError {
fn from(src: RetryClientError) -> Self {
match src {
RetryClientError::ProviderError(err) => err,
RetryClientError::TimeoutError => ProviderError::JsonRpcClientError(Box::new(src)),
RetryClientError::SerdeJson(err) => err.into(),
}
}
}

Expand All @@ -236,7 +227,7 @@ where
T: JsonRpcClient + 'static,
T::Error: Sync + Send + 'static,
{
type Error = RetryClientError<T>;
type Error = RetryClientError;

async fn request<A, R>(&self, method: &str, params: A) -> Result<R, Self::Error>
where
Expand All @@ -254,8 +245,7 @@ where
let params = if std::mem::size_of::<A>() == 0 {
RetryParams::Zst(())
} else {
let params =
serde_json::to_value(params).map_err(|err| RetryClientError::SerdeJson(err))?;
let params = serde_json::to_value(params).map_err(RetryClientError::SerdeJson)?;
RetryParams::Value(params)
};

Expand Down Expand Up @@ -320,6 +310,7 @@ where
trace!("retrying and backing off for {:?}", next_backoff);
tokio::time::sleep(next_backoff).await;
} else {
let err: ProviderError = err.into();
if timeout_retries < self.timeout_retries && maybe_connectivity(&err) {
timeout_retries += 1;
trace!(err = ?err, "retrying due to spurious network");
Expand Down Expand Up @@ -379,8 +370,8 @@ fn compute_unit_offset_in_secs(

/// Checks whether the `error` is the result of a connectivity issue, like
/// `request::Error::TimedOut`
fn maybe_connectivity(err: &(dyn Error + 'static)) -> bool {
if let Some(reqwest_err) = err.downcast_ref::<reqwest::Error>() {
fn maybe_connectivity(err: &ProviderError) -> bool {
if let ProviderError::HTTPError(reqwest_err) = err {
if reqwest_err.is_timeout() || reqwest_err.is_connect() {
return true
}
Expand Down

0 comments on commit 10f74f8

Please sign in to comment.