pub struct FallbackConfigBuilder<Req, Res, E> { /* private fields */ }Expand description
Builder for constructing a FallbackLayer.
Implementations§
Source§impl<Req, Res, E> FallbackConfigBuilder<Req, Res, E>
impl<Req, Res, E> FallbackConfigBuilder<Req, Res, E>
Sourcepub fn name(self, name: impl Into<String>) -> Self
pub fn name(self, name: impl Into<String>) -> Self
Sets the name for this fallback instance (used in metrics and events).
Sourcepub fn value(self, value: Res) -> Selfwhere
Res: Clone,
pub fn value(self, value: Res) -> Selfwhere
Res: Clone,
Sets a static fallback value.
Note: This requires Res: Clone. If your response type doesn’t implement
Clone, use value_fn instead.
Sourcepub fn value_fn<F>(self, f: F) -> Self
pub fn value_fn<F>(self, f: F) -> Self
Sets a fallback value generator function.
Unlike value, this doesn’t require Res: Clone since
the function generates a fresh value for each fallback.
§Example
use tower_resilience_fallback::FallbackLayer;
let layer: FallbackLayer<String, MyResponse, MyError> = FallbackLayer::builder()
.value_fn(|| MyResponse { data: vec![0; 1024] })
.build();Sourcepub fn from_error<F>(self, f: F) -> Self
pub fn from_error<F>(self, f: F) -> Self
Sets a fallback function that computes a response from the error.
Sourcepub fn from_request_error<F>(self, f: F) -> Self
pub fn from_request_error<F>(self, f: F) -> Self
Sets a fallback function that has access to both request and error.
Sourcepub fn handle<F>(self, predicate: F) -> Self
pub fn handle<F>(self, predicate: F) -> Self
Only trigger fallback for errors matching this predicate.
Errors that don’t match the predicate will be propagated as-is.
Sourcepub fn handle_response<F>(self, predicate: F) -> Self
pub fn handle_response<F>(self, predicate: F) -> Self
Trigger fallback for successful responses matching this predicate.
When this predicate returns true for a response, the fallback strategy
is applied as if the service had returned an error. This is useful when
errors are encoded inside successful responses, such as:
- JSON-RPC error codes in the response body
- HTTP 200 responses containing error payloads
- Responses indicating degraded or stale data
§Example
use tower_resilience_fallback::FallbackLayer;
#[derive(Debug, Clone)]
struct MyError;
#[derive(Clone)]
struct ApiResponse {
is_stale: bool,
data: String,
}
let layer: FallbackLayer<String, ApiResponse, MyError> = FallbackLayer::builder()
.value_fn(|| ApiResponse { is_stale: false, data: "default".to_string() })
.handle_response(|resp: &ApiResponse| resp.is_stale)
.build();