[{"data":1,"prerenderedAt":700},["ShallowReactive",2],{"$f51ldIG01BhhkVVTvEHIwBUxYiQA_RAmOORdlGof53Ws":3},{"slug":4,"title":5,"description":6,"source":7,"document":8,"toc":687,"editUrl":694,"prev":695,"next":698},"overview\u002Finterceptors","Interceptors","Wrap the handler with before\u002Fafter logic using the Interceptor trait and","---\ntitle: Interceptors\ndescription: Wrap the handler with before\u002Fafter logic using the Interceptor trait and #[use_interceptors].\n---\n\n# Interceptors\n\nInterceptors wrap **around** a handler: they run a \"before\" phase, invoke the rest of the pipeline, and run an \"after\" phase on the produced response. Use them for caching responses, timing handlers, mutating results, or adding response headers.\n\n## The `Interceptor` trait\n\n```rust\nuse ferrite_framework::{Interceptor, RequestCtx, Next, Response, async_trait};\n\n#[derive(Default)]\npub struct LoggingInterceptor;\n\n#[async_trait]\nimpl Interceptor for LoggingInterceptor {\n    async fn intercept(&self, ctx: RequestCtx, next: Next) -> Response {\n        eprintln!(\"before handler: {}\", ctx.uri());\n        let response = next(ctx).await;\n        eprintln!(\"after handler: status {}\", response.status());\n        response\n    }\n}\n```\n\n## Applying interceptors\n\n```rust\n#[impl_controller]\nimpl UsersController {\n    #[use_interceptors(LoggingInterceptor)]\n    #[get(\"\u002F\")]\n    pub async fn list(&self) -> Result\u003CJson\u003CVec\u003CUser>>, HttpError> { \u002F* ... *\u002F }\n}\n```\n\nBlock-level application works too:\n\n```rust\n#[impl_controller]\n#[use_interceptors(LoggingInterceptor)]\nimpl UsersController { \u002F* ... *\u002F }\n```\n\nInterceptors compose: declare multiple and they wrap the handler in order.\n\n## A caching interceptor\n\nBecause the interceptor sees the raw `Response`, a trivial in-process cache is easy:\n\n```rust\n#[async_trait]\nimpl Interceptor for CachingInterceptor {\n    async fn intercept(&self, ctx: RequestCtx, next: Next) -> Response {\n        if let Some(hit) = self.cache.get(&ctx.uri().to_string()) {\n            return axum::response::Response::new(hit);\n        }\n        let response = next(ctx).await;\n        self.cache.put(ctx.uri().to_string(), response.body().to_owned());\n        response\n    }\n}\n```\n\n> For battle-tested caching, use `ferrite-cache-redis` `CacheService` — see [Caching (Redis)](\u002Fdocs\u002Ftechniques\u002Fcaching).\n\n## Pipeline position\n\n```text\nMiddleware → Guards → Interceptors(before) → Pipes → Handler → Interceptors(after) → Response\n```\n\nThe \"after\" phase is where you can rewrite the response. Pipes do run inside the \"before\" phase (they transform inputs ahead of the handler).\n\n## Next steps\n\n- [Pipes](\u002Fdocs\u002Foverview\u002Fpipes) — transform inputs before the handler.\n- [Exception filters](\u002Fdocs\u002Foverview\u002Fexception-filters).",{"frontmatter":9,"meta":10,"nodes":31},{"title":5,"description":6},{"toc":11},{"title":12,"searchDepth":13,"depth":14,"links":15},"On This Page",2,3,[16,19,22,25,28],{"id":17,"depth":13,"text":18},"the-interceptor-trait","The Interceptor trait",{"id":20,"depth":13,"text":21},"applying-interceptors","Applying interceptors",{"id":23,"depth":13,"text":24},"a-caching-interceptor","A caching interceptor",{"id":26,"depth":13,"text":27},"pipeline-position","Pipeline position",{"id":29,"depth":13,"text":30},"next-steps","Next steps",[32,36,45,54,281,283,383,386,416,419,421,427,635,652,654,665,668,670],[33,34,5],"h1",{"id":35},"interceptors",[37,38,39,40,44],"p",{},"Interceptors wrap ",[41,42,43],"strong",{},"around"," a handler: they run a \"before\" phase, invoke the rest of the pipeline, and run an \"after\" phase on the produced response. Use them for caching responses, timing handlers, mutating results, or adding response headers.",[46,47,48,49,53],"h2",{"id":17},"The ",[50,51,52],"code",{},"Interceptor"," trait",[55,56,59],"pre",{"language":57,"class":58},"rust","shiki shiki-themes github-light github-dark dark:github-dark",[50,60,62,103,104,103,106,103,117,103,131,103,133,103,138,103,154,103,203,103,225,103,247,103,266,103,271,103,276],{"class":61},"language-rust",[63,64,67,71,75,78,82,84,87,90,92,95,97,100],"span",{"class":65,"style":66},"line","display: inline",[63,68,70],{"style":69},"color:#D73A49;--shiki-dark:#F97583","use",[63,72,74],{"style":73},"color:#6F42C1;--shiki-dark:#B392F0"," ferrite_framework",[63,76,77],{"style":69},"::",[63,79,81],{"style":80},"color:#24292E;--shiki-dark:#E1E4E8","{",[63,83,52],{"style":73},[63,85,86],{"style":80},", ",[63,88,89],{"style":73},"RequestCtx",[63,91,86],{"style":80},[63,93,94],{"style":73},"Next",[63,96,86],{"style":80},[63,98,99],{"style":73},"Response",[63,101,102],{"style":80},", async_trait};","\n",[63,105],{"class":65,"style":66},[63,107,108,111,114],{"class":65,"style":66},[63,109,110],{"style":80},"#[derive(",[63,112,113],{"style":73},"Default",[63,115,116],{"style":80},")]",[63,118,119,122,125,128],{"class":65,"style":66},[63,120,121],{"style":69},"pub",[63,123,124],{"style":69}," struct",[63,126,127],{"style":73}," LoggingInterceptor",[63,129,130],{"style":80},";",[63,132],{"class":65,"style":66},[63,134,135],{"class":65,"style":66},[63,136,137],{"style":80},"#[async_trait]",[63,139,140,143,146,149,151],{"class":65,"style":66},[63,141,142],{"style":69},"impl",[63,144,145],{"style":73}," Interceptor",[63,147,148],{"style":69}," for",[63,150,127],{"style":73},[63,152,153],{"style":80}," {",[63,155,156,159,162,165,168,171,175,178,181,184,187,189,192,195,198,201],{"class":65,"style":66},[63,157,158],{"style":69},"    async",[63,160,161],{"style":69}," fn",[63,163,164],{"style":73}," intercept",[63,166,167],{"style":80},"(",[63,169,170],{"style":69},"&",[63,172,174],{"style":173},"color:#005CC5;--shiki-dark:#79B8FF","self",[63,176,177],{"style":80},", ctx",[63,179,180],{"style":69},":",[63,182,183],{"style":73}," RequestCtx",[63,185,186],{"style":80},", next",[63,188,180],{"style":69},[63,190,191],{"style":73}," Next",[63,193,194],{"style":80},") ",[63,196,197],{"style":69},"->",[63,199,200],{"style":73}," Response",[63,202,153],{"style":80},[63,204,205,208,210,214,216,219,222],{"class":65,"style":66},[63,206,207],{"style":73},"        eprintln!",[63,209,167],{"style":80},[63,211,213],{"style":212},"color:#032F62;--shiki-dark:#9ECBFF","\"before handler: {}\"",[63,215,177],{"style":80},[63,217,218],{"style":69},".",[63,220,221],{"style":73},"uri",[63,223,224],{"style":80},"());",[63,226,227,230,233,236,239,242,245],{"class":65,"style":66},[63,228,229],{"style":69},"        let",[63,231,232],{"style":80}," response ",[63,234,235],{"style":69},"=",[63,237,238],{"style":73}," next",[63,240,241],{"style":80},"(ctx)",[63,243,244],{"style":69},".await",[63,246,130],{"style":80},[63,248,249,251,253,256,259,261,264],{"class":65,"style":66},[63,250,207],{"style":73},[63,252,167],{"style":80},[63,254,255],{"style":212},"\"after handler: status {}\"",[63,257,258],{"style":80},", response",[63,260,218],{"style":69},[63,262,263],{"style":73},"status",[63,265,224],{"style":80},[63,267,268],{"class":65,"style":66},[63,269,270],{"style":80},"        response",[63,272,273],{"class":65,"style":66},[63,274,275],{"style":80},"    }",[63,277,278],{"class":65,"style":66},[63,279,280],{"style":80},"}",[46,282,21],{"id":20},[55,284,285],{"language":57,"class":58},[50,286,287,103,292,103,301,103,311,103,321,103,379],{"class":61},[63,288,289],{"class":65,"style":66},[63,290,291],{"style":80},"#[impl_controller]",[63,293,294,296,299],{"class":65,"style":66},[63,295,142],{"style":69},[63,297,298],{"style":73}," UsersController",[63,300,153],{"style":80},[63,302,303,306,309],{"class":65,"style":66},[63,304,305],{"style":80},"    #[use_interceptors(",[63,307,308],{"style":73},"LoggingInterceptor",[63,310,116],{"style":80},[63,312,313,316,319],{"class":65,"style":66},[63,314,315],{"style":80},"    #[get(",[63,317,318],{"style":212},"\"\u002F\"",[63,320,116],{"style":80},[63,322,323,326,329,331,334,336,338,340,342,344,347,350,353,355,358,360,363,366,369,372,376],{"class":65,"style":66},[63,324,325],{"style":69},"    pub",[63,327,328],{"style":69}," async",[63,330,161],{"style":69},[63,332,333],{"style":73}," list",[63,335,167],{"style":80},[63,337,170],{"style":69},[63,339,174],{"style":173},[63,341,194],{"style":80},[63,343,197],{"style":69},[63,345,346],{"style":73}," Result",[63,348,349],{"style":80},"\u003C",[63,351,352],{"style":73},"Json",[63,354,349],{"style":80},[63,356,357],{"style":73},"Vec",[63,359,349],{"style":80},[63,361,362],{"style":73},"User",[63,364,365],{"style":80},">>, ",[63,367,368],{"style":73},"HttpError",[63,370,371],{"style":80},"> { ",[63,373,375],{"style":374},"color:#6A737D;--shiki-dark:#6A737D","\u002F* ... *\u002F",[63,377,378],{"style":80}," }",[63,380,381],{"class":65,"style":66},[63,382,280],{"style":80},[37,384,385],{},"Block-level application works too:",[55,387,388],{"language":57,"class":58},[50,389,390,103,394,103,403],{"class":61},[63,391,392],{"class":65,"style":66},[63,393,291],{"style":80},[63,395,396,399,401],{"class":65,"style":66},[63,397,398],{"style":80},"#[use_interceptors(",[63,400,308],{"style":73},[63,402,116],{"style":80},[63,404,405,407,409,412,414],{"class":65,"style":66},[63,406,142],{"style":69},[63,408,298],{"style":73},[63,410,411],{"style":80}," { ",[63,413,375],{"style":374},[63,415,378],{"style":80},[37,417,418],{},"Interceptors compose: declare multiple and they wrap the handler in order.",[46,420,24],{"id":23},[37,422,423,424,426],{},"Because the interceptor sees the raw ",[50,425,99],{},", a trivial in-process cache is easy:",[55,428,429],{"language":57,"class":58},[50,430,431,103,435,103,448,103,482,103,533,103,558,103,563,103,579,103,623,103,627,103,631],{"class":61},[63,432,433],{"class":65,"style":66},[63,434,137],{"style":80},[63,436,437,439,441,443,446],{"class":65,"style":66},[63,438,142],{"style":69},[63,440,145],{"style":73},[63,442,148],{"style":69},[63,444,445],{"style":73}," CachingInterceptor",[63,447,153],{"style":80},[63,449,450,452,454,456,458,460,462,464,466,468,470,472,474,476,478,480],{"class":65,"style":66},[63,451,158],{"style":69},[63,453,161],{"style":69},[63,455,164],{"style":73},[63,457,167],{"style":80},[63,459,170],{"style":69},[63,461,174],{"style":173},[63,463,177],{"style":80},[63,465,180],{"style":69},[63,467,183],{"style":73},[63,469,186],{"style":80},[63,471,180],{"style":69},[63,473,191],{"style":73},[63,475,194],{"style":80},[63,477,197],{"style":69},[63,479,200],{"style":73},[63,481,153],{"style":80},[63,483,484,487,490,493,496,498,501,503,506,508,511,513,515,518,520,522,525,527,530],{"class":65,"style":66},[63,485,486],{"style":69},"        if",[63,488,489],{"style":69}," let",[63,491,492],{"style":73}," Some",[63,494,495],{"style":80},"(hit) ",[63,497,235],{"style":69},[63,499,500],{"style":173}," self",[63,502,218],{"style":69},[63,504,505],{"style":80},"cache",[63,507,218],{"style":69},[63,509,510],{"style":73},"get",[63,512,167],{"style":80},[63,514,170],{"style":69},[63,516,517],{"style":80},"ctx",[63,519,218],{"style":69},[63,521,221],{"style":73},[63,523,524],{"style":80},"()",[63,526,218],{"style":69},[63,528,529],{"style":73},"to_string",[63,531,532],{"style":80},"()) {",[63,534,535,538,541,543,546,548,550,552,555],{"class":65,"style":66},[63,536,537],{"style":69},"            return",[63,539,540],{"style":73}," axum",[63,542,77],{"style":69},[63,544,545],{"style":73},"response",[63,547,77],{"style":69},[63,549,99],{"style":73},[63,551,77],{"style":69},[63,553,554],{"style":73},"new",[63,556,557],{"style":80},"(hit);",[63,559,560],{"class":65,"style":66},[63,561,562],{"style":80},"        }",[63,564,565,567,569,571,573,575,577],{"class":65,"style":66},[63,566,229],{"style":69},[63,568,232],{"style":80},[63,570,235],{"style":69},[63,572,238],{"style":73},[63,574,241],{"style":80},[63,576,244],{"style":69},[63,578,130],{"style":80},[63,580,581,584,586,588,590,593,596,598,600,602,604,606,609,611,614,616,618,621],{"class":65,"style":66},[63,582,583],{"style":173},"        self",[63,585,218],{"style":69},[63,587,505],{"style":80},[63,589,218],{"style":69},[63,591,592],{"style":73},"put",[63,594,595],{"style":80},"(ctx",[63,597,218],{"style":69},[63,599,221],{"style":73},[63,601,524],{"style":80},[63,603,218],{"style":69},[63,605,529],{"style":73},[63,607,608],{"style":80},"(), response",[63,610,218],{"style":69},[63,612,613],{"style":73},"body",[63,615,524],{"style":80},[63,617,218],{"style":69},[63,619,620],{"style":73},"to_owned",[63,622,224],{"style":80},[63,624,625],{"class":65,"style":66},[63,626,270],{"style":80},[63,628,629],{"class":65,"style":66},[63,630,275],{"style":80},[63,632,633],{"class":65,"style":66},[63,634,280],{"style":80},[636,637,638,639,642,643,646,647,218],"blockquote",{},"For battle-tested caching, use ",[50,640,641],{},"ferrite-cache-redis"," ",[50,644,645],{},"CacheService"," — see ",[648,649,651],"a",{"href":650},"\u002Fdocs\u002Ftechniques\u002Fcaching","Caching (Redis)",[46,653,27],{"id":26},[55,655,657],{"language":656,"class":58},"text",[50,658,660],{"class":659},"language-text",[63,661,662],{"class":65,"style":66},[63,663,664],{},"Middleware → Guards → Interceptors(before) → Pipes → Handler → Interceptors(after) → Response",[37,666,667],{},"The \"after\" phase is where you can rewrite the response. Pipes do run inside the \"before\" phase (they transform inputs ahead of the handler).",[46,669,30],{"id":29},[671,672,673,681],"ul",{},[674,675,676,680],"li",{},[648,677,679],{"href":678},"\u002Fdocs\u002Foverview\u002Fpipes","Pipes"," — transform inputs before the handler.",[674,682,683,218],{},[648,684,686],{"href":685},"\u002Fdocs\u002Foverview\u002Fexception-filters","Exception filters",{"title":12,"searchDepth":13,"depth":14,"links":688},[689,690,691,692,693],{"id":17,"depth":13,"text":18},{"id":20,"depth":13,"text":21},{"id":23,"depth":13,"text":24},{"id":26,"depth":13,"text":27},{"id":29,"depth":13,"text":30},"https:\u002F\u002Fgithub.com\u002Fj4flmao\u002Fferrite_rs\u002Fedit\u002Fmain\u002Fdocs\u002Fcontent\u002Foverview\u002Finterceptors.md",{"title":696,"slug":697},"Guards","overview\u002Fguards",{"title":679,"slug":699},"overview\u002Fpipes",1789565440545]