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

Callers 1

not_foundFunction · 0.70

Calls 1

renderMethod · 0.80

Tested by

no test coverage detected