Adding error handling so that if a blog or project is requested that does not exist the server does not crash

This commit is contained in:
Awstin 2024-07-12 09:22:07 -04:00
parent 323a868b83
commit 649a09c1a8
3 changed files with 16 additions and 12 deletions

View file

@ -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<AppState>,
Path(params): Path<HashMap<String, String>>,
) -> impl IntoResponse {
) -> Result<impl IntoResponse, StatusCode> {
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<Vec<String>, Box<dyn Error>> {
let mut articles: Vec<Article> = 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));

View file

@ -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<AppState>,
Path(params): Path<HashMap<String, String>>,
) -> impl IntoResponse {
) -> Result<impl IntoResponse, StatusCode> {
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 = "<a href=\"/projects\">Back to Projects</a>";
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<Vec<String>, Box<dyn Error>> {
let mut projects: Vec<Project> = 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));

6
templates/404.html Normal file
View file

@ -0,0 +1,6 @@
<!-- prettier-ignore -->
{% extends "base.html" %}
{% block content %}
<h1>Sorry the requested content could not be found</h1>
{% endblock %}