fix: handle provider error correctly (#1630)

* fix: handle provider error correctly

* chore(clippy): make clippy happy

* chore: rustfmt

* convert http error
This commit is contained in:
Matthias Seitz 2022-08-22 22:47:43 +02:00 committed by GitHub
parent 7c26550064
commit 10f74f818a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 25 deletions

View File

@ -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)),
}
}
}

View File

@ -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,
@ -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(),
}
}
}
@ -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
@ -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)
};
@ -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");
@ -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
}