From dd67b521998c5dccee2d68eb2894803a9f6cce58 Mon Sep 17 00:00:00 2001 From: spikecodes <19519553+spikecodes@users.noreply.github.com> Date: Sat, 20 Feb 2021 18:36:30 -0800 Subject: [PATCH] Fix #126 --- src/main.rs | 4 ++-- src/post.rs | 2 +- src/search.rs | 2 +- src/subreddit.rs | 4 ++-- src/user.rs | 2 +- src/utils.rs | 9 ++------- 6 files changed, 9 insertions(+), 14 deletions(-) diff --git a/src/main.rs b/src/main.rs index 0853618..fe22945 100644 --- a/src/main.rs +++ b/src/main.rs @@ -246,12 +246,12 @@ async fn main() -> tide::Result<()> { Ok("best") | Ok("hot") | Ok("new") | Ok("top") | Ok("rising") | Ok("controversial") => subreddit::page(req).await, // Short link for post Ok(id) if id.len() > 4 && id.len() < 7 => post::item(req).await, - _ => error("Nothing here".to_string()).await, + _ => error(req, "Nothing here".to_string()).await, } }); // Default service in case no routes match - app.at("*").get(|_| error("Nothing here".to_string())); + app.at("*").get(|req| error(req, "Nothing here".to_string())); app.listen(listener).await?; Ok(()) diff --git a/src/post.rs b/src/post.rs index 7308e01..91899cb 100644 --- a/src/post.rs +++ b/src/post.rs @@ -58,7 +58,7 @@ pub async fn item(req: Request<()>) -> tide::Result { }) } // If the Reddit API returns an error, exit and send error page to user - Err(msg) => error(msg).await, + Err(msg) => error(req, msg).await, } } diff --git a/src/search.rs b/src/search.rs index 1771988..f8dd4c2 100644 --- a/src/search.rs +++ b/src/search.rs @@ -65,7 +65,7 @@ pub async fn find(req: Request<()>) -> tide::Result { }, prefs: prefs(req), }), - Err(msg) => error(msg).await, + Err(msg) => error(req, msg).await, } } diff --git a/src/subreddit.rs b/src/subreddit.rs index 69114a8..9c05809 100644 --- a/src/subreddit.rs +++ b/src/subreddit.rs @@ -74,7 +74,7 @@ pub async fn page(req: Request<()>) -> tide::Result { prefs: prefs(req), }) } - Err(msg) => error(msg).await, + Err(msg) => error(req, msg).await, } } @@ -140,7 +140,7 @@ pub async fn wiki(req: Request<()>) -> tide::Result { page, prefs: prefs(req), }), - Err(msg) => error(msg).await, + Err(msg) => error(req, msg).await, } } diff --git a/src/user.rs b/src/user.rs index 223e96f..ba426d2 100644 --- a/src/user.rs +++ b/src/user.rs @@ -41,7 +41,7 @@ pub async fn profile(req: Request<()>) -> tide::Result { }) } // If there is an error show error page - Err(msg) => error(msg).await, + Err(msg) => error(req, msg).await, } } diff --git a/src/utils.rs b/src/utils.rs index ea1b831..e1e0837 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -495,13 +495,8 @@ pub fn redirect(path: String) -> Response { .build() } -pub async fn error(msg: String) -> tide::Result { - let body = ErrorTemplate { - msg, - prefs: Preferences::default(), - } - .render() - .unwrap_or_default(); +pub async fn error(req: Request<()>, msg: String) -> tide::Result { + let body = ErrorTemplate { msg, prefs: prefs(req) }.render().unwrap_or_default(); Ok(Response::builder(404).content_type("text/html").body(body).build()) }