pub struct FallibleLayer<L> { /* private fields */ }Expand description
A tower layer whose service can fail, made into one the app can mount
(AppRouter::layer,
Endpoint::layer,
Router::route_layer): the
service’s error becomes a 500 AppError with the error as its source,
so the app’s error mapping
(m.map_error) and JSON error
shape apply; its response body is taken as the app’s body.
The service is cloned for each request and asked first whether it is
ready: an error there (a load shedder’s, say) answers like an error of
the call. Without a mapping the client sees "Internal server error",
and the error is logged, never sent. That holds in every
LayerSlot but Edge, which sits outside the app’s
error rendering: there the error answers as a 500 in the JSON error
shape, neither mapped nor logged.
use std::time::Duration;
use navree_http::{AppError, FallibleLayer, GlobalLayer, LayerSlot, ModuleBuilderHttpExt};
use navree_kernel::{Module, ModuleBuilder};
struct LedgerModule;
impl Module for LedgerModule {
fn register(&self, m: &mut ModuleBuilder) {
m.http_layer(GlobalLayer::new(LayerSlot::Context, "timeout", |_, router| {
Ok(router.layer(FallibleLayer::new(tower::timeout::TimeoutLayer::new(
Duration::from_secs(10),
))))
}))
// The timeout's error answers 503 instead of 500.
.map_error::<tower::timeout::error::Elapsed>(|_| {
Some(AppError::service_unavailable("The khata took too long."))
});
}
}Implementations§
Source§impl<L> FallibleLayer<L>
impl<L> FallibleLayer<L>
Sourcepub fn new(layer: L) -> FallibleLayer<L>
pub fn new(layer: L) -> FallibleLayer<L>
Wraps layer, whose service may answer with an error.