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:
parent
323a868b83
commit
649a09c1a8
3 changed files with 16 additions and 12 deletions
|
|
@ -1,11 +1,8 @@
|
||||||
use achubb_database::data::{article::Article, PsqlData};
|
use achubb_database::data::{article::Article, PsqlData};
|
||||||
use askama::Template;
|
use askama::Template;
|
||||||
use axum::{
|
use axum::{
|
||||||
extract::{Extension, Path},
|
extract::{Extension, Path}, http::StatusCode, response::IntoResponse, routing::{get, Router}
|
||||||
response::IntoResponse,
|
|
||||||
routing::{get, Router},
|
|
||||||
};
|
};
|
||||||
use core::panic;
|
|
||||||
use sqlx::PgPool;
|
use sqlx::PgPool;
|
||||||
use std::{collections::HashMap, error::Error};
|
use std::{collections::HashMap, error::Error};
|
||||||
|
|
||||||
|
|
@ -44,12 +41,12 @@ struct BlogFooterTemplate {
|
||||||
async fn article(
|
async fn article(
|
||||||
state: Extension<AppState>,
|
state: Extension<AppState>,
|
||||||
Path(params): Path<HashMap<String, String>>,
|
Path(params): Path<HashMap<String, String>>,
|
||||||
) -> impl IntoResponse {
|
) -> Result<impl IntoResponse, StatusCode> {
|
||||||
let db_pool = &state.db;
|
let db_pool = &state.db;
|
||||||
let article_id: &String = params.get("article").unwrap();
|
let article_id: &String = params.get("article").unwrap();
|
||||||
let article: Article = match Article::read_by_reference(db_pool, article_id).await {
|
let article: Article = match Article::read_by_reference(db_pool, article_id).await {
|
||||||
Ok(a) => *a,
|
Ok(a) => *a,
|
||||||
Err(_) => panic!("Not an article at all!!!!"),
|
Err(_) => return Err(StatusCode::NOT_FOUND),
|
||||||
};
|
};
|
||||||
let footer = BlogFooterTemplate {
|
let footer = BlogFooterTemplate {
|
||||||
previous: match article.previous {
|
previous: match article.previous {
|
||||||
|
|
@ -65,13 +62,13 @@ async fn article(
|
||||||
content: article.content,
|
content: article.content,
|
||||||
footer: footer.to_string(),
|
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>> {
|
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 {
|
let mut articles: Vec<Article> = match Article::read_all(pool).await {
|
||||||
Ok(a) => a.iter().map(|x| *x.clone()).collect(),
|
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));
|
articles.sort_by(|a, b| b.date.cmp(&a.date));
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
use achubb_database::data::{project::Project, PsqlData};
|
use achubb_database::data::{project::Project, PsqlData};
|
||||||
use askama::Template;
|
use askama::Template;
|
||||||
use axum::extract::{Extension, Path};
|
use axum::extract::{Extension, Path};
|
||||||
|
use axum::http::StatusCode;
|
||||||
use axum::response::IntoResponse;
|
use axum::response::IntoResponse;
|
||||||
use axum::{routing::get, Router};
|
use axum::{routing::get, Router};
|
||||||
use sqlx::PgPool;
|
use sqlx::PgPool;
|
||||||
|
|
@ -34,12 +35,12 @@ struct ProjectsTemplate {
|
||||||
async fn project(
|
async fn project(
|
||||||
state: Extension<AppState>,
|
state: Extension<AppState>,
|
||||||
Path(params): Path<HashMap<String, String>>,
|
Path(params): Path<HashMap<String, String>>,
|
||||||
) -> impl IntoResponse {
|
) -> Result<impl IntoResponse, StatusCode> {
|
||||||
let db_pool = &state.db;
|
let db_pool = &state.db;
|
||||||
let project_id: &String = params.get("project").unwrap();
|
let project_id: &String = params.get("project").unwrap();
|
||||||
let project: Project = match Project::read_by_reference(db_pool, project_id).await {
|
let project: Project = match Project::read_by_reference(db_pool, project_id).await {
|
||||||
Ok(a) => *a,
|
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 footer: &str = "<a href=\"/projects\">Back to Projects</a>";
|
||||||
let template = ArticleTemplate {
|
let template = ArticleTemplate {
|
||||||
|
|
@ -48,13 +49,13 @@ async fn project(
|
||||||
.expect("Should have had content if it got this far"),
|
.expect("Should have had content if it got this far"),
|
||||||
footer: footer.to_string(),
|
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>> {
|
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 {
|
let mut projects: Vec<Project> = match Project::read_all(pool).await {
|
||||||
Ok(a) => a.iter().map(|x| *x.clone()).collect(),
|
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));
|
projects.sort_by(|a, b| b.date.cmp(&a.date));
|
||||||
|
|
|
||||||
6
templates/404.html
Normal file
6
templates/404.html
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
<!-- prettier-ignore -->
|
||||||
|
{% extends "base.html" %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
<h1>Sorry the requested content could not be found</h1>
|
||||||
|
{% endblock %}
|
||||||
Loading…
Reference in a new issue