[{"data":1,"prerenderedAt":799},["ShallowReactive",2],{"$fjD0rQ5db_Giw4dxucwbgr03Pu5Y3NkqBdI65PXji_aQ":3},{"slug":4,"title":5,"description":6,"source":7,"document":8,"toc":785,"editUrl":793,"prev":794,"next":796},"overview\u002Fexception-filters","Exception filters","Centralize error responses with the ExceptionFilter trait and","---\ntitle: Exception filters\ndescription: Centralize error responses with the ExceptionFilter trait and #[catch].\n---\n\n# Exception filters\n\nException filters give you a single place to turn errors into HTTP responses. When a handler (or any downstream pipeline step) returns an `Err(HttpError)`, the pipeline looks for an attached exception filter and hands it the error.\n\n## The `ExceptionFilter` trait\n\n```rust\nuse ferrite_framework::{ExceptionFilter, HttpError, Response, axum};\n\npub struct AllExceptionsFilter {\n    pub include_stack: bool,\n}\n\nimpl ExceptionFilter for AllExceptionsFilter {\n    fn catch(&self, err: HttpError) -> Response {\n        axum::Json(serde_json::json!({\n            \"statusCode\": err.status.as_u16(),\n            \"message\": err.message,\n            \"stack\": err.message,\n        }))\n        .into_response()\n    }\n}\n```\n\nNote that `catch` is **synchronous** (unlike guards and interceptors).\n\n## Attaching filters\n\n```rust\n#[impl_controller]\nimpl UsersController {\n    #[catch(AllExceptionsFilter { include_stack: true })]\n    #[get(\"\u002F{id}\")]\n    pub async fn show(&self, Path(id): Path\u003Ci64>) -> Result\u003CJson\u003CUser>, HttpError> {\n        let user = self.service.find(id)\n            .await?\n            .ok_or_else(|| HttpError::not_found(\"user\"))?;\n        Ok(Json(user))\n    }\n}\n```\n\nA filter instantiation expression (like `AllExceptionsFilter { include_stack: true }`) is attached per route. When the handler errors, `catch` builds the response.\n\n## Default error body\n\nWithout a filter, `HttpError` renders as:\n\n```json\n{ \"statusCode\": 404, \"message\": \"user not found\" }\n```\n\n## HttpError\n\nConstructors cover the common cases:\n\n```rust\nHttpError::new(StatusCode::IM_A_TEAPOT, \"tea time\")\nHttpError::bad_request(\"...\")     \u002F\u002F 400\nHttpError::unauthorized(\"...\")    \u002F\u002F 401\nHttpError::forbidden(\"...\")       \u002F\u002F 403\nHttpError::not_found(\"...\")       \u002F\u002F 404\nHttpError::conflict(\"...\")        \u002F\u002F 409\nHttpError::internal(\"...\")        \u002F\u002F 500\n```\n\n`HttpResult\u003CT>` is a convenience alias:\n\n```rust\npub type HttpResult\u003CT> = Result\u003CT, HttpError>;\n```\n\n## Pipeline\n\n```text\nMiddleware → Guards → Interceptors(before) → Pipes → Handler → Interceptors(after) → Response\n                         └────────────── Exception Filter on error ──────────────┘\n```\n\nFilters only run when a step returns `Err(HttpError)`; the intercepted response and after-phase interceptors still execute for successes.\n\n## Next steps\n\n- [Pipes](\u002Fdocs\u002Foverview\u002Fpipes).\n- [Controllers](\u002Fdocs\u002Foverview\u002Fcontrollers).",{"frontmatter":9,"meta":10,"nodes":34},{"title":5,"description":6},{"toc":11},{"title":12,"searchDepth":13,"depth":14,"links":15},"On This Page",2,3,[16,19,22,25,28,31],{"id":17,"depth":13,"text":18},"the-exceptionfilter-trait","The ExceptionFilter trait",{"id":20,"depth":13,"text":21},"attaching-filters","Attaching filters",{"id":23,"depth":13,"text":24},"default-error-body","Default error body",{"id":26,"depth":13,"text":27},"httperror","HttpError",{"id":29,"depth":13,"text":30},"pipeline","Pipeline",{"id":32,"depth":13,"text":33},"next-steps","Next steps",[35,39,48,56,292,304,306,499,509,511,517,550,552,555,699,705,742,744,760,766,768],[36,37,5],"h1",{"id":38},"exception-filters",[40,41,42,43,47],"p",{},"Exception filters give you a single place to turn errors into HTTP responses. When a handler (or any downstream pipeline step) returns an ",[44,45,46],"code",{},"Err(HttpError)",", the pipeline looks for an attached exception filter and hands it the error.",[49,50,51,52,55],"h2",{"id":17},"The ",[44,53,54],{},"ExceptionFilter"," trait",[57,58,61],"pre",{"language":59,"class":60},"rust","shiki shiki-themes github-light github-dark dark:github-dark",[44,62,64,99,100,99,102,99,116,99,133,99,138,99,140,99,155,99,192,99,215,99,240,99,254,99,267,99,272,99,283,99,288],{"class":63},"language-rust",[65,66,69,73,77,80,84,86,89,91,93,96],"span",{"class":67,"style":68},"line","display: inline",[65,70,72],{"style":71},"color:#D73A49;--shiki-dark:#F97583","use",[65,74,76],{"style":75},"color:#6F42C1;--shiki-dark:#B392F0"," ferrite_framework",[65,78,79],{"style":71},"::",[65,81,83],{"style":82},"color:#24292E;--shiki-dark:#E1E4E8","{",[65,85,54],{"style":75},[65,87,88],{"style":82},", ",[65,90,27],{"style":75},[65,92,88],{"style":82},[65,94,95],{"style":75},"Response",[65,97,98],{"style":82},", axum};","\n",[65,101],{"class":67,"style":68},[65,103,104,107,110,113],{"class":67,"style":68},[65,105,106],{"style":71},"pub",[65,108,109],{"style":71}," struct",[65,111,112],{"style":75}," AllExceptionsFilter",[65,114,115],{"style":82}," {",[65,117,118,121,124,127,130],{"class":67,"style":68},[65,119,120],{"style":71},"    pub",[65,122,123],{"style":82}," include_stack",[65,125,126],{"style":71},":",[65,128,129],{"style":75}," bool",[65,131,132],{"style":82},",",[65,134,135],{"class":67,"style":68},[65,136,137],{"style":82},"}",[65,139],{"class":67,"style":68},[65,141,142,145,148,151,153],{"class":67,"style":68},[65,143,144],{"style":71},"impl",[65,146,147],{"style":75}," ExceptionFilter",[65,149,150],{"style":71}," for",[65,152,112],{"style":75},[65,154,115],{"style":82},[65,156,157,160,163,166,169,173,176,178,181,184,187,190],{"class":67,"style":68},[65,158,159],{"style":71},"    fn",[65,161,162],{"style":75}," catch",[65,164,165],{"style":82},"(",[65,167,168],{"style":71},"&",[65,170,172],{"style":171},"color:#005CC5;--shiki-dark:#79B8FF","self",[65,174,175],{"style":82},", err",[65,177,126],{"style":71},[65,179,180],{"style":75}," HttpError",[65,182,183],{"style":82},") ",[65,185,186],{"style":71},"->",[65,188,189],{"style":75}," Response",[65,191,115],{"style":82},[65,193,194,197,199,202,204,207,209,212],{"class":67,"style":68},[65,195,196],{"style":75},"        axum",[65,198,79],{"style":71},[65,200,201],{"style":75},"Json",[65,203,165],{"style":82},[65,205,206],{"style":75},"serde_json",[65,208,79],{"style":71},[65,210,211],{"style":75},"json!",[65,213,214],{"style":82},"({",[65,216,217,221,223,226,229,232,234,237],{"class":67,"style":68},[65,218,220],{"style":219},"color:#032F62;--shiki-dark:#9ECBFF","            \"statusCode\"",[65,222,126],{"style":71},[65,224,225],{"style":82}," err",[65,227,228],{"style":71},".",[65,230,231],{"style":82},"status",[65,233,228],{"style":71},[65,235,236],{"style":75},"as_u16",[65,238,239],{"style":82},"(),",[65,241,242,245,247,249,251],{"class":67,"style":68},[65,243,244],{"style":219},"            \"message\"",[65,246,126],{"style":71},[65,248,225],{"style":82},[65,250,228],{"style":71},[65,252,253],{"style":82},"message,",[65,255,256,259,261,263,265],{"class":67,"style":68},[65,257,258],{"style":219},"            \"stack\"",[65,260,126],{"style":71},[65,262,225],{"style":82},[65,264,228],{"style":71},[65,266,253],{"style":82},[65,268,269],{"class":67,"style":68},[65,270,271],{"style":82},"        }))",[65,273,274,277,280],{"class":67,"style":68},[65,275,276],{"style":71},"        .",[65,278,279],{"style":75},"into_response",[65,281,282],{"style":82},"()",[65,284,285],{"class":67,"style":68},[65,286,287],{"style":82},"    }",[65,289,290],{"class":67,"style":68},[65,291,137],{"style":82},[40,293,294,295,298,299,303],{},"Note that ",[44,296,297],{},"catch"," is ",[300,301,302],"strong",{},"synchronous"," (unlike guards and interceptors).",[49,305,21],{"id":20},[57,307,308],{"language":59,"class":60},[44,309,310,99,315,99,324,99,340,99,351,99,414,99,440,99,445,99,479,99,491,99,495],{"class":63},[65,311,312],{"class":67,"style":68},[65,313,314],{"style":82},"#[impl_controller]",[65,316,317,319,322],{"class":67,"style":68},[65,318,144],{"style":71},[65,320,321],{"style":75}," UsersController",[65,323,115],{"style":82},[65,325,326,329,332,335,337],{"class":67,"style":68},[65,327,328],{"style":82},"    #[catch(",[65,330,331],{"style":75},"AllExceptionsFilter",[65,333,334],{"style":82}," { include_stack",[65,336,126],{"style":71},[65,338,339],{"style":82}," true })]",[65,341,342,345,348],{"class":67,"style":68},[65,343,344],{"style":82},"    #[get(",[65,346,347],{"style":219},"\"\u002F{id}\"",[65,349,350],{"style":82},")]",[65,352,353,355,358,361,364,366,368,370,372,375,378,380,383,386,389,392,394,397,399,401,403,406,409,411],{"class":67,"style":68},[65,354,120],{"style":71},[65,356,357],{"style":71}," async",[65,359,360],{"style":71}," fn",[65,362,363],{"style":75}," show",[65,365,165],{"style":82},[65,367,168],{"style":71},[65,369,172],{"style":171},[65,371,88],{"style":82},[65,373,374],{"style":75},"Path",[65,376,377],{"style":82},"(id)",[65,379,126],{"style":71},[65,381,382],{"style":75}," Path",[65,384,385],{"style":82},"\u003C",[65,387,388],{"style":75},"i64",[65,390,391],{"style":82},">) ",[65,393,186],{"style":71},[65,395,396],{"style":75}," Result",[65,398,385],{"style":82},[65,400,201],{"style":75},[65,402,385],{"style":82},[65,404,405],{"style":75},"User",[65,407,408],{"style":82},">, ",[65,410,27],{"style":75},[65,412,413],{"style":82},"> {",[65,415,416,419,422,425,428,430,433,435,438],{"class":67,"style":68},[65,417,418],{"style":71},"        let",[65,420,421],{"style":82}," user ",[65,423,424],{"style":71},"=",[65,426,427],{"style":171}," self",[65,429,228],{"style":71},[65,431,432],{"style":82},"service",[65,434,228],{"style":71},[65,436,437],{"style":75},"find",[65,439,377],{"style":82},[65,441,442],{"class":67,"style":68},[65,443,444],{"style":71},"            .await?",[65,446,447,450,453,455,458,460,462,465,467,470,473,476],{"class":67,"style":68},[65,448,449],{"style":71},"            .",[65,451,452],{"style":75},"ok_or_else",[65,454,165],{"style":82},[65,456,457],{"style":71},"||",[65,459,180],{"style":75},[65,461,79],{"style":71},[65,463,464],{"style":75},"not_found",[65,466,165],{"style":82},[65,468,469],{"style":219},"\"user\"",[65,471,472],{"style":82},"))",[65,474,475],{"style":71},"?",[65,477,478],{"style":82},";",[65,480,481,484,486,488],{"class":67,"style":68},[65,482,483],{"style":75},"        Ok",[65,485,165],{"style":82},[65,487,201],{"style":75},[65,489,490],{"style":82},"(user))",[65,492,493],{"class":67,"style":68},[65,494,287],{"style":82},[65,496,497],{"class":67,"style":68},[65,498,137],{"style":82},[40,500,501,502,505,506,508],{},"A filter instantiation expression (like ",[44,503,504],{},"AllExceptionsFilter { include_stack: true }",") is attached per route. When the handler errors, ",[44,507,297],{}," builds the response.",[49,510,24],{"id":23},[40,512,513,514,516],{},"Without a filter, ",[44,515,27],{}," renders as:",[57,518,520],{"language":519,"class":60},"json",[44,521,523],{"class":522},"language-json",[65,524,525,528,531,534,537,539,542,544,547],{"class":67,"style":68},[65,526,527],{"style":82},"{ ",[65,529,530],{"style":171},"\"statusCode\"",[65,532,533],{"style":82},": ",[65,535,536],{"style":171},"404",[65,538,88],{"style":82},[65,540,541],{"style":171},"\"message\"",[65,543,533],{"style":82},[65,545,546],{"style":219},"\"user not found\"",[65,548,549],{"style":82}," }",[49,551,27],{"id":26},[40,553,554],{},"Constructors cover the common cases:",[57,556,557],{"language":59,"class":60},[44,558,559,99,586,99,607,99,626,99,645,99,662,99,681],{"class":63},[65,560,561,563,565,568,570,573,575,578,580,583],{"class":67,"style":68},[65,562,27],{"style":75},[65,564,79],{"style":71},[65,566,567],{"style":75},"new",[65,569,165],{"style":82},[65,571,572],{"style":75},"StatusCode",[65,574,79],{"style":71},[65,576,577],{"style":171},"IM_A_TEAPOT",[65,579,88],{"style":82},[65,581,582],{"style":219},"\"tea time\"",[65,584,585],{"style":82},")",[65,587,588,590,592,595,597,600,603],{"class":67,"style":68},[65,589,27],{"style":75},[65,591,79],{"style":71},[65,593,594],{"style":75},"bad_request",[65,596,165],{"style":82},[65,598,599],{"style":219},"\"...\"",[65,601,602],{"style":82},")     ",[65,604,606],{"style":605},"color:#6A737D;--shiki-dark:#6A737D","\u002F\u002F 400",[65,608,609,611,613,616,618,620,623],{"class":67,"style":68},[65,610,27],{"style":75},[65,612,79],{"style":71},[65,614,615],{"style":75},"unauthorized",[65,617,165],{"style":82},[65,619,599],{"style":219},[65,621,622],{"style":82},")    ",[65,624,625],{"style":605},"\u002F\u002F 401",[65,627,628,630,632,635,637,639,642],{"class":67,"style":68},[65,629,27],{"style":75},[65,631,79],{"style":71},[65,633,634],{"style":75},"forbidden",[65,636,165],{"style":82},[65,638,599],{"style":219},[65,640,641],{"style":82},")       ",[65,643,644],{"style":605},"\u002F\u002F 403",[65,646,647,649,651,653,655,657,659],{"class":67,"style":68},[65,648,27],{"style":75},[65,650,79],{"style":71},[65,652,464],{"style":75},[65,654,165],{"style":82},[65,656,599],{"style":219},[65,658,641],{"style":82},[65,660,661],{"style":605},"\u002F\u002F 404",[65,663,664,666,668,671,673,675,678],{"class":67,"style":68},[65,665,27],{"style":75},[65,667,79],{"style":71},[65,669,670],{"style":75},"conflict",[65,672,165],{"style":82},[65,674,599],{"style":219},[65,676,677],{"style":82},")        ",[65,679,680],{"style":605},"\u002F\u002F 409",[65,682,683,685,687,690,692,694,696],{"class":67,"style":68},[65,684,27],{"style":75},[65,686,79],{"style":71},[65,688,689],{"style":75},"internal",[65,691,165],{"style":82},[65,693,599],{"style":219},[65,695,677],{"style":82},[65,697,698],{"style":605},"\u002F\u002F 500",[40,700,701,704],{},[44,702,703],{},"HttpResult\u003CT>"," is a convenience alias:",[57,706,707],{"language":59,"class":60},[44,708,709],{"class":63},[65,710,711,713,716,719,721,724,727,729,731,733,735,737,739],{"class":67,"style":68},[65,712,106],{"style":71},[65,714,715],{"style":71}," type",[65,717,718],{"style":75}," HttpResult",[65,720,385],{"style":82},[65,722,723],{"style":75},"T",[65,725,726],{"style":82},"> ",[65,728,424],{"style":71},[65,730,396],{"style":75},[65,732,385],{"style":82},[65,734,723],{"style":75},[65,736,88],{"style":82},[65,738,27],{"style":75},[65,740,741],{"style":82},">;",[49,743,30],{"id":29},[57,745,747],{"language":746,"class":60},"text",[44,748,750,99,755],{"class":749},"language-text",[65,751,752],{"class":67,"style":68},[65,753,754],{},"Middleware → Guards → Interceptors(before) → Pipes → Handler → Interceptors(after) → Response",[65,756,757],{"class":67,"style":68},[65,758,759],{},"                         └────────────── Exception Filter on error ──────────────┘",[40,761,762,763,765],{},"Filters only run when a step returns ",[44,764,46],{},"; the intercepted response and after-phase interceptors still execute for successes.",[49,767,33],{"id":32},[769,770,771,779],"ul",{},[772,773,774,228],"li",{},[775,776,778],"a",{"href":777},"\u002Fdocs\u002Foverview\u002Fpipes","Pipes",[772,780,781,228],{},[775,782,784],{"href":783},"\u002Fdocs\u002Foverview\u002Fcontrollers","Controllers",{"title":12,"searchDepth":13,"depth":14,"links":786},[787,788,789,790,791,792],{"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},{"id":32,"depth":13,"text":33},"https:\u002F\u002Fgithub.com\u002Fj4flmao\u002Fferrite_rs\u002Fedit\u002Fmain\u002Fdocs\u002Fcontent\u002Foverview\u002Fexception-filters.md",{"title":778,"slug":795},"overview\u002Fpipes",{"title":797,"slug":798},"Configuration","overview\u002Fconfiguration",1789565440597]