1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
use actix_web::{
body::{BoxBody, EitherBody, MessageBody},
dev::ServiceResponse,
http::{header, StatusCode},
middleware::ErrorHandlerResponse,
};
use crate::templates;
pub fn render_404(res: ServiceResponse) -> actix_web::Result<ErrorHandlerResponse<BoxBody>> {
Ok(error_response(
res,
StatusCode::NOT_FOUND,
"The resource you requested can't be found.",
))
}
pub fn render_500(res: ServiceResponse) -> actix_web::Result<ErrorHandlerResponse<BoxBody>> {
Ok(error_response(
res,
StatusCode::INTERNAL_SERVER_ERROR,
"Sorry, Something went wrong. This is probably not your fault.",
))
}
fn error_response(
mut res: ServiceResponse,
status_code: StatusCode,
message: &str,
) -> ErrorHandlerResponse<BoxBody> {
res.headers_mut().insert(
header::CONTENT_TYPE,
header::HeaderValue::from_static(mime::TEXT_HTML_UTF_8.as_ref()),
);
ErrorHandlerResponse::Response(res.map_body(|_head, _body| {
EitherBody::right(MessageBody::boxed(
render!(templates::error_html, status_code, message).unwrap(),
))
}))
}
|