Generic error handler.
(res: &ServiceResponse<B>, error: &str)
| 108 | |
| 109 | // Generic error handler. |
| 110 | fn 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 | } |