summaryrefslogtreecommitdiffstats
path: root/src/routes/error.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/routes/error.rs')
-rw-r--r--src/routes/error.rs40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/routes/error.rs b/src/routes/error.rs
new file mode 100644
index 0000000..543b8a8
--- /dev/null
+++ b/src/routes/error.rs
@@ -0,0 +1,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(),
+ ))
+ }))
+}