diff --git a/src/robust_provider/provider.rs b/src/robust_provider/provider.rs index 2a4227b0..1fe17e08 100644 --- a/src/robust_provider/provider.rs +++ b/src/robust_provider/provider.rs @@ -9,6 +9,7 @@ use alloy::{ transports::{RpcError, TransportErrorKind}, }; use backon::{ExponentialBuilder, Retryable}; +use futures::TryFutureExt; use thiserror::Error; use tokio::time::{error as TokioError, timeout}; use tracing::{error, info}; @@ -28,7 +29,7 @@ pub enum Error { /// Low-level error related to RPC calls and failover logic. #[derive(Error, Debug)] -pub(crate) enum CoreError { +pub enum CoreError { #[error("Operation timed out")] Timeout, #[error("RPC call failed after exhausting all retry attempts: {0}")] @@ -315,7 +316,7 @@ impl RobustProvider { /// * Returns [`RpcError::Transport(TransportErrorKind::PubsubUnavailable)`] if `require_pubsub` /// is true and all providers don't support pubsub. /// * Propagates any [`RpcError`] from the underlying retries. - pub(crate) async fn try_operation_with_failover( + pub async fn try_operation_with_failover( &self, operation: F, require_pubsub: bool, @@ -325,15 +326,11 @@ impl RobustProvider { Fut: Future>>, { let primary = self.primary(); - let result = self.try_provider_with_timeout(primary, &operation).await; - - if result.is_ok() { - return result; - } - - let last_error = result.unwrap_err(); - - self.try_fallback_providers(&operation, require_pubsub, last_error).await + self.try_provider_with_timeout(primary, &operation) + .or_else(|last_error| { + self.try_fallback_providers(&operation, require_pubsub, last_error) + }) + .await } pub(crate) async fn try_fallback_providers(