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:
parent
7c26550064
commit
10f74f818a
|
@ -52,7 +52,10 @@ pub enum ClientError {
|
||||||
|
|
||||||
impl From<ClientError> for ProviderError {
|
impl From<ClientError> for ProviderError {
|
||||||
fn from(src: ClientError) -> Self {
|
fn from(src: ClientError) -> Self {
|
||||||
ProviderError::JsonRpcClientError(Box::new(src))
|
match src {
|
||||||
|
ClientError::ReqwestError(err) => ProviderError::HTTPError(err),
|
||||||
|
_ => ProviderError::JsonRpcClientError(Box::new(src)),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,6 @@ use crate::{provider::ProviderError, JsonRpcClient};
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use serde::{de::DeserializeOwned, Serialize};
|
use serde::{de::DeserializeOwned, Serialize};
|
||||||
use std::{
|
use std::{
|
||||||
error::Error,
|
|
||||||
fmt::Debug,
|
fmt::Debug,
|
||||||
sync::atomic::{AtomicU32, Ordering},
|
sync::atomic::{AtomicU32, Ordering},
|
||||||
time::Duration,
|
time::Duration,
|
||||||
|
@ -198,35 +197,27 @@ impl Default for RetryClientBuilder {
|
||||||
/// 2. Params serialization failed.
|
/// 2. Params serialization failed.
|
||||||
/// 3. Request timed out i.e. max retries were already made.
|
/// 3. Request timed out i.e. max retries were already made.
|
||||||
#[derive(Error, Debug)]
|
#[derive(Error, Debug)]
|
||||||
pub enum RetryClientError<T>
|
pub enum RetryClientError {
|
||||||
where
|
|
||||||
T: JsonRpcClient,
|
|
||||||
T::Error: Sync + Send + 'static,
|
|
||||||
{
|
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
ProviderError(T::Error),
|
ProviderError(ProviderError),
|
||||||
TimeoutError,
|
TimeoutError,
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
SerdeJson(serde_json::Error),
|
SerdeJson(serde_json::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> std::fmt::Display for RetryClientError<T>
|
impl std::fmt::Display for RetryClientError {
|
||||||
where
|
|
||||||
T: JsonRpcClient,
|
|
||||||
<T as JsonRpcClient>::Error: Sync + Send + 'static,
|
|
||||||
{
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
write!(f, "{:?}", self)
|
write!(f, "{:?}", self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> From<RetryClientError<T>> for ProviderError
|
impl From<RetryClientError> for ProviderError {
|
||||||
where
|
fn from(src: RetryClientError) -> Self {
|
||||||
T: JsonRpcClient + 'static,
|
match src {
|
||||||
<T as JsonRpcClient>::Error: Sync + Send + 'static,
|
RetryClientError::ProviderError(err) => err,
|
||||||
{
|
RetryClientError::TimeoutError => ProviderError::JsonRpcClientError(Box::new(src)),
|
||||||
fn from(src: RetryClientError<T>) -> Self {
|
RetryClientError::SerdeJson(err) => err.into(),
|
||||||
ProviderError::JsonRpcClientError(Box::new(src))
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -236,7 +227,7 @@ where
|
||||||
T: JsonRpcClient + 'static,
|
T: JsonRpcClient + 'static,
|
||||||
T::Error: Sync + Send + '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>
|
async fn request<A, R>(&self, method: &str, params: A) -> Result<R, Self::Error>
|
||||||
where
|
where
|
||||||
|
@ -254,8 +245,7 @@ where
|
||||||
let params = if std::mem::size_of::<A>() == 0 {
|
let params = if std::mem::size_of::<A>() == 0 {
|
||||||
RetryParams::Zst(())
|
RetryParams::Zst(())
|
||||||
} else {
|
} else {
|
||||||
let params =
|
let params = serde_json::to_value(params).map_err(RetryClientError::SerdeJson)?;
|
||||||
serde_json::to_value(params).map_err(|err| RetryClientError::SerdeJson(err))?;
|
|
||||||
RetryParams::Value(params)
|
RetryParams::Value(params)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -320,6 +310,7 @@ where
|
||||||
trace!("retrying and backing off for {:?}", next_backoff);
|
trace!("retrying and backing off for {:?}", next_backoff);
|
||||||
tokio::time::sleep(next_backoff).await;
|
tokio::time::sleep(next_backoff).await;
|
||||||
} else {
|
} else {
|
||||||
|
let err: ProviderError = err.into();
|
||||||
if timeout_retries < self.timeout_retries && maybe_connectivity(&err) {
|
if timeout_retries < self.timeout_retries && maybe_connectivity(&err) {
|
||||||
timeout_retries += 1;
|
timeout_retries += 1;
|
||||||
trace!(err = ?err, "retrying due to spurious network");
|
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
|
/// Checks whether the `error` is the result of a connectivity issue, like
|
||||||
/// `request::Error::TimedOut`
|
/// `request::Error::TimedOut`
|
||||||
fn maybe_connectivity(err: &(dyn Error + 'static)) -> bool {
|
fn maybe_connectivity(err: &ProviderError) -> bool {
|
||||||
if let Some(reqwest_err) = err.downcast_ref::<reqwest::Error>() {
|
if let ProviderError::HTTPError(reqwest_err) = err {
|
||||||
if reqwest_err.is_timeout() || reqwest_err.is_connect() {
|
if reqwest_err.is_timeout() || reqwest_err.is_connect() {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue