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(),
))
}))
}