Skip to content

Commit 5f594f4

Browse files
authored
Refactor try_operation_with_failover and Expose It and CoreError publicly from RobustProvider (#271)
1 parent 87b7362 commit 5f594f4

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

src/robust_provider/provider.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use alloy::{
99
transports::{RpcError, TransportErrorKind},
1010
};
1111
use backon::{ExponentialBuilder, Retryable};
12+
use futures::TryFutureExt;
1213
use thiserror::Error;
1314
use tokio::time::{error as TokioError, timeout};
1415
use tracing::{error, info};
@@ -28,7 +29,7 @@ pub enum Error {
2829

2930
/// Low-level error related to RPC calls and failover logic.
3031
#[derive(Error, Debug)]
31-
pub(crate) enum CoreError {
32+
pub enum CoreError {
3233
#[error("Operation timed out")]
3334
Timeout,
3435
#[error("RPC call failed after exhausting all retry attempts: {0}")]
@@ -315,7 +316,7 @@ impl<N: Network> RobustProvider<N> {
315316
/// * Returns [`RpcError::Transport(TransportErrorKind::PubsubUnavailable)`] if `require_pubsub`
316317
/// is true and all providers don't support pubsub.
317318
/// * Propagates any [`RpcError<TransportErrorKind>`] from the underlying retries.
318-
pub(crate) async fn try_operation_with_failover<T: Debug, F, Fut>(
319+
pub async fn try_operation_with_failover<T: Debug, F, Fut>(
319320
&self,
320321
operation: F,
321322
require_pubsub: bool,
@@ -325,15 +326,11 @@ impl<N: Network> RobustProvider<N> {
325326
Fut: Future<Output = Result<T, RpcError<TransportErrorKind>>>,
326327
{
327328
let primary = self.primary();
328-
let result = self.try_provider_with_timeout(primary, &operation).await;
329-
330-
if result.is_ok() {
331-
return result;
332-
}
333-
334-
let last_error = result.unwrap_err();
335-
336-
self.try_fallback_providers(&operation, require_pubsub, last_error).await
329+
self.try_provider_with_timeout(primary, &operation)
330+
.or_else(|last_error| {
331+
self.try_fallback_providers(&operation, require_pubsub, last_error)
332+
})
333+
.await
337334
}
338335

339336
pub(crate) async fn try_fallback_providers<T: Debug, F, Fut>(

0 commit comments

Comments
 (0)