pub struct MapErr<F, S, E> { /* private fields */ }Expand description
Transforms call and readiness errors with a synchronous function.
Created by Service::map_err and crate::ServiceChain::map_err.
The function receives an inner error and returns the new error value.
Successful responses are returned unchanged without invoking the function.
Request and state types are also unchanged.
This is useful for giving composed services a common error type. It does
not recover errors into responses; use crate::ServiceChain::then for
call-result recovery.
Binding and shutdown are forwarded to the inner service. Readiness is
forwarded with the same error conversion as calls. Mapping preserves the
inner service’s readiness identity without adding an independent cache or
an extra readiness check at call time. An explicit crate::ready()
wrapper retains its own readiness behavior.
Cloning clones both the inner service and mapping function. Whether their state is shared depends on their clone implementations.
§Example
use ntex_service::{Service, fn_service};
let pipeline = fn_service(async |req: u32| {
if req == 0 { Err("zero is not allowed") } else { Ok(req) }
})
.map_err(|error| format!("request failed: {error}"))
.pipeline(());
assert_eq!(pipeline.call(1).await, Ok(1));
assert_eq!(
pipeline.call(0).await,
Err(String::from("request failed: zero is not allowed")),
);