summaryrefslogtreecommitdiffstats
path: root/src/routes/error.rs
diff options
context:
space:
mode:
authorLibravatar cel 🌸 <cel@blos.sm>2023-12-12 14:14:30 +0000
committerLibravatar cel 🌸 <cel@blos.sm>2023-12-12 14:14:30 +0000
commit370a25e5a0cbb95e2aa1cec55305b22aeaf99aa0 (patch)
tree665163bc58c8e94320b843b1983376d9a67f99bf /src/routes/error.rs
parent5dc4774ed3380762b4d7aadc86193af6073c456a (diff)
downloadpinussy-370a25e5a0cbb95e2aa1cec55305b22aeaf99aa0.tar.gz
pinussy-370a25e5a0cbb95e2aa1cec55305b22aeaf99aa0.tar.bz2
pinussy-370a25e5a0cbb95e2aa1cec55305b22aeaf99aa0.zip
initial refactor
Diffstat (limited to '')
-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(),
+ ))
+ }))
+}