Interceptors
Wrap the handler, run code on both sides of it, or answer in its place.
An interceptor wraps the handler, which suits work that needs both the call and its result, such as timing, logging or mapping a reply. It runs once an admitted call has built its interceptors, calls next.run() to reach the rest of the chain and the handler, and sees the result on the way back. The example for this page is examples/16-http-enhancers.
The trait
pub trait Interceptor<T: Transport>: Send + Sync + 'static {
fn intercept(&self, cx: &T::Cx, next: Next<'_, T>) -> impl Future<Output = Result<T::Reply, BoxError>> + Send;
}next.run() consumes the Next, so the rest of the chain runs at most once. An interceptor that returns without calling it answers the call itself and the handler never runs.
Stamp is the example's own type: a tier name and a Dep<Trace>, where Trace is a provider that records what ran.
use ulo::{BoxError, Interceptor, Next};
use ulo_http::{Http, HttpCx, Response};
impl Interceptor<Http> for Stamp {
async fn intercept(&self, _cx: &HttpCx, next: Next<'_, Http>) -> Result<Response, BoxError> {
self.trace.push(format!("{} in", self.tier));
let reply = next.run().await;
self.trace.push(format!("{} out", self.tier));
reply
}
}Stamp in the example implements Guard<Http> as well, so one type serves both roles. A type implements a role for each transport it serves: Interceptor<Http> and Interceptor<Rpc> on one struct make it usable on handlers of both.
Nesting order
Interceptors nest in the same order guards run. Global is outermost, then controller, then method, and within a tier the order written. For /reports/payroll the example's trace reads:
global in, controller in, method in, handler, method out, controller out, global outInterceptors are built only after every guard has admitted, so a refused call constructs none.
What an interceptor sees
next.run() answers the handler's reply or its error. The error arrives as it was raised, as a domain error not yet rendered. The interceptor can return it, replace it or turn it into a reply.
The error handlers sit outside the interceptors. An interceptor sees the Err on its way out and never sees the reply an error handler produced for it. A stamp that must reach every answer, a recovered error included, belongs in a middleware or a tower layer, which see the final response.
A panic in the handler or in an inner interceptor arrives at the interceptor around it as Err holding PanicRecovered. State the panicking stage touched may be inconsistent. Treat that Err as fatal for the call and do not retry.
On HTTP the reply of a streaming handler is returned without pulling the stream, so an interceptor sees the response head and not the events.
Declaring interceptors
An interceptor is declared the way a guard is: an into AnyInterceptor<Http> entry in the module for a global one, and #[interceptors(..)] on the impl or the handler.
#[handlers]
#[interceptors(with = |trace: Dep<Trace>| Stamp::new("controller", trace))]
impl ReportsController {
#[ulo_http::get("/reports/payroll")]
#[interceptors(with = |trace: Dep<Trace>| Stamp::new("method", trace))]
fn payroll(&self) -> &'static str { /* .. */ }
}#[module(providers = [
into AnyInterceptor<Http>: [with = |trace: Dep<Trace>| Stamp::new("global", trace)],
/* .. */
])]Request Lifecycle lists the spellings and what each builds.