From 649a09c1a89ec50a1de72aa7743679b19c334f23 Mon Sep 17 00:00:00 2001 From: Awstin Date: Fri, 12 Jul 2024 09:22:07 -0400 Subject: [PATCH] Adding error handling so that if a blog or project is requested that does not exist the server does not crash --- src/html/blog.rs | 13 +++++-------- src/html/projects.rs | 9 +++++---- templates/404.html | 6 ++++++ 3 files changed, 16 insertions(+), 12 deletions(-) create mode 100644 templates/404.html diff --git a/src/html/blog.rs b/src/html/blog.rs index e341363..35bd148 100644 --- a/src/html/blog.rs +++ b/src/html/blog.rs @@ -1,11 +1,8 @@ use achubb_database::data::{article::Article, PsqlData}; use askama::Template; use axum::{ - extract::{Extension, Path}, - response::IntoResponse, - routing::{get, Router}, + extract::{Extension, Path}, http::StatusCode, response::IntoResponse, routing::{get, Router} }; -use core::panic; use sqlx::PgPool; use std::{collections::HashMap, error::Error}; @@ -44,12 +41,12 @@ struct BlogFooterTemplate { async fn article( state: Extension, Path(params): Path>, -) -> impl IntoResponse { +) -> Result { let db_pool = &state.db; let article_id: &String = params.get("article").unwrap(); let article: Article = match Article::read_by_reference(db_pool, article_id).await { Ok(a) => *a, - Err(_) => panic!("Not an article at all!!!!"), + Err(_) => return Err(StatusCode::NOT_FOUND), }; let footer = BlogFooterTemplate { previous: match article.previous { @@ -65,13 +62,13 @@ async fn article( content: article.content, footer: footer.to_string(), }; - HtmlTemplate(template) + Ok(HtmlTemplate(template)) } pub async fn get_articles_as_links_list(pool: &PgPool) -> Result, Box> { let mut articles: Vec
= match Article::read_all(pool).await { Ok(a) => a.iter().map(|x| *x.clone()).collect(), - Err(_) => panic!("Not an article at all!!!!"), + Err(_) => Vec::new(), }; articles.sort_by(|a, b| b.date.cmp(&a.date)); diff --git a/src/html/projects.rs b/src/html/projects.rs index 77d8748..afe9f68 100644 --- a/src/html/projects.rs +++ b/src/html/projects.rs @@ -1,6 +1,7 @@ use achubb_database::data::{project::Project, PsqlData}; use askama::Template; use axum::extract::{Extension, Path}; +use axum::http::StatusCode; use axum::response::IntoResponse; use axum::{routing::get, Router}; use sqlx::PgPool; @@ -34,12 +35,12 @@ struct ProjectsTemplate { async fn project( state: Extension, Path(params): Path>, -) -> impl IntoResponse { +) -> Result { let db_pool = &state.db; let project_id: &String = params.get("project").unwrap(); let project: Project = match Project::read_by_reference(db_pool, project_id).await { Ok(a) => *a, - Err(_) => panic!("Not an article at all!!!!"), + Err(_) => return Err(StatusCode::NOT_FOUND), }; let footer: &str = "Back to Projects"; let template = ArticleTemplate { @@ -48,13 +49,13 @@ async fn project( .expect("Should have had content if it got this far"), footer: footer.to_string(), }; - HtmlTemplate(template) + Ok(HtmlTemplate(template)) } pub async fn get_projects_as_links_list(pool: &PgPool) -> Result, Box> { let mut projects: Vec = match Project::read_all(pool).await { Ok(a) => a.iter().map(|x| *x.clone()).collect(), - Err(_) => panic!("Not a project at all!!!!"), + Err(_) => Vec::new(), }; projects.sort_by(|a, b| b.date.cmp(&a.date)); diff --git a/templates/404.html b/templates/404.html new file mode 100644 index 0000000..4909530 --- /dev/null +++ b/templates/404.html @@ -0,0 +1,6 @@ + +{% extends "base.html" %} + +{% block content %} +

Sorry the requested content could not be found

+{% endblock %}