MCPcopy Create free account
hub / github.com/actix/examples / get_error_response

Function get_error_response

templating/fluent/src/main.rs:110–138  ·  view source on GitHub ↗

Generic error handler.

(res: &ServiceResponse<B>, error: &str)

Source from the content-addressed store, hash-verified

108// Generic error handler.
109fn get_error_response<B>(res: &ServiceResponse<B>, error: &str) -> HttpResponse<BoxBody> {
110 let req = res.request();
111 let lang = LangChoice::from_req(req);
112
113 // Provide a fallback to a simple plain text response in case an error occurs during the
114 // rendering of the error page.
115
116 let hb = req
117 .app_data::<web::Data<Handlebars>>()
118 .expect("Correctly set up handlebars in app data");
119
120 let data = json!({
121 "lang": lang,
122 "error": error,
123 "status_code": res.status().as_str()
124 });
125
126 let body = hb.render("error", &data);
127
128 match body {
129 Ok(body) => HttpResponse::build(res.status())
130 .content_type(ContentType::html())
131 .body(body),
132
133 Err(_) => HttpResponse::build(res.status())
134 .content_type(ContentType::plaintext())
135 .body(error.to_string()),
136 }
137}

Callers 1

not_foundFunction · 0.70

Calls 1

renderMethod · 0.80

Tested by

no test coverage detected