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

Function get_error_response

templating/handlebars/src/main.rs:85–116  ·  view source on GitHub ↗

Generic error handler.

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

Source from the content-addressed store, hash-verified

83}
84
85// Generic error handler.
86fn get_error_response<B>(res: &ServiceResponse<B>, error: &str) -> HttpResponse<BoxBody> {
87 let request = res.request();
88
89 // Provide a fallback to a simple plain text response in case an error occurs during the
90 // rendering of the error page.
91 let fallback = |err: &str| {
92 HttpResponse::build(res.status())
93 .content_type(ContentType::plaintext())
94 .body(err.to_string())
95 };
96
97 let hb = request
98 .app_data::<web::Data<Handlebars>>()
99 .map(|t| t.get_ref());
100 match hb {
101 Some(hb) => {
102 let data = json!({
103 "error": error,
104 "status_code": res.status().as_str()
105 });
106 let body = hb.render("error", &data);
107
108 match body {
109 Ok(body) => HttpResponse::build(res.status())
110 .content_type(ContentType::html())
111 .body(body),
112 Err(_) => fallback(error),
113 }
114 }
115 None => fallback(error),
116 }
117}

Callers 1

not_foundFunction · 0.70

Calls 1

renderMethod · 0.80

Tested by

no test coverage detected