From 9cfe8dd123bbf310302198cd515fb8951accf73c Mon Sep 17 00:00:00 2001 From: Dan <39170265+chillenberger@users.noreply.github.com> Date: Thu, 21 Dec 2023 09:12:05 -0700 Subject: [PATCH 1/2] actually make this 400 for now --- pgml-dashboard/src/api/cms.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/pgml-dashboard/src/api/cms.rs b/pgml-dashboard/src/api/cms.rs index a5cc975f2..9cc4b4893 100644 --- a/pgml-dashboard/src/api/cms.rs +++ b/pgml-dashboard/src/api/cms.rs @@ -40,10 +40,7 @@ pub struct Document { impl Document { pub async fn from_path(path: &PathBuf) -> anyhow::Result { - let contents = match tokio::fs::read_to_string(&path).await { - Ok(contents) => contents, - Err(_) => String::from("

Failed to find your requested document!

"), - }; + let contents = tokio::fs::read_to_string(&path).await?; let parts = contents.split("---").collect::>(); From ca9add5edb1713b9536699982b7f8007f167abf6 Mon Sep 17 00:00:00 2001 From: Dan <39170265+chillenberger@users.noreply.github.com> Date: Thu, 21 Dec 2023 11:10:35 -0700 Subject: [PATCH 2/2] add 404 response with context --- pgml-dashboard/src/api/cms.rs | 71 +++++++++++++++++++++++++---------- 1 file changed, 52 insertions(+), 19 deletions(-) diff --git a/pgml-dashboard/src/api/cms.rs b/pgml-dashboard/src/api/cms.rs index 9cc4b4893..d9b2e2acb 100644 --- a/pgml-dashboard/src/api/cms.rs +++ b/pgml-dashboard/src/api/cms.rs @@ -3,12 +3,7 @@ use std::path::{Path, PathBuf}; use comrak::{format_html_with_plugins, parse_document, Arena, ComrakPlugins}; use lazy_static::lazy_static; use markdown::mdast::Node; -use rocket::{ - fs::NamedFile, - http::{uri::Origin, Status}, - route::Route, - State, -}; +use rocket::{fs::NamedFile, http::uri::Origin, route::Route, State}; use yaml_rust::YamlLoader; use crate::{ @@ -39,6 +34,17 @@ pub struct Document { } impl Document { + pub fn new(content: &str) -> Document { + Document { + path: PathBuf::new(), + description: None, + image: None, + title: "404".to_string(), + toc_links: Vec::new(), + html: content.to_string(), + } + } + pub async fn from_path(path: &PathBuf) -> anyhow::Result { let contents = tokio::fs::read_to_string(&path).await?; @@ -153,7 +159,7 @@ impl Collection { mut path: PathBuf, cluster: &Cluster, origin: &Origin<'_>, - ) -> Result { + ) -> Result { info!("get_content: {} | {path:?}", self.name); if origin.path().ends_with("/") { @@ -270,17 +276,21 @@ impl Collection { } // renders document in layout - async fn render<'a>(&self, path: &'a PathBuf, cluster: &Cluster) -> Result { + async fn render<'a>( + &self, + path: &'a PathBuf, + cluster: &Cluster, + ) -> Result { + let user = if cluster.context.user.is_anonymous() { + None + } else { + Some(cluster.context.user.clone()) + }; + match Document::from_path(&path).await { Ok(doc) => { let index = self.open_index(doc.path); - let user = if cluster.context.user.is_anonymous() { - None - } else { - Some(cluster.context.user.clone()) - }; - let mut layout = crate::templates::Layout::new(&doc.title, Some(cluster)); if let Some(image) = doc.image { layout.image(&config::asset_url(image.into())); @@ -303,7 +313,30 @@ impl Collection { )) } // Return page not found on bad path - _ => Err(Status::NotFound), + _ => { + let mut layout = crate::templates::Layout::new("404", None); + + let doc = crate::api::cms::Document::new( + r#" +
+

Oops, document not found!

+

The document you are searching for may have been moved or replaced with better content.

+
"#, + ); + + if let Some(user) = &user { + layout.user(user); + } + + layout + .nav_links(&self.index) + .nav_title(&self.name) + .footer(cluster.context.marketing_footer.to_string()); + + layout.render(crate::templates::Article { content: doc.html }); + + Err(crate::responses::NotFound(layout.into())) + } } } } @@ -341,7 +374,7 @@ async fn get_blog( path: PathBuf, cluster: &Cluster, origin: &Origin<'_>, -) -> Result { +) -> Result { BLOG.get_content(path, cluster, origin).await } @@ -350,7 +383,7 @@ async fn get_careers( path: PathBuf, cluster: &Cluster, origin: &Origin<'_>, -) -> Result { +) -> Result { CAREERS.get_content(path, cluster, origin).await } @@ -359,7 +392,7 @@ async fn get_docs( path: PathBuf, cluster: &Cluster, origin: &Origin<'_>, -) -> Result { +) -> Result { DOCS.get_content(path, cluster, origin).await } @@ -547,7 +580,7 @@ This is the end of the markdown let rsp = req.dispatch().await; assert!( - rsp.status() == Status::Ok, + rsp.status() == Status::NotFound, "Returned status {:?}", rsp.status() );