From 09a7bdfc7614da103d27b10d11395049ee9d42d3 Mon Sep 17 00:00:00 2001 From: gabatxo1312 Date: Fri, 30 Jan 2026 10:33:25 +0100 Subject: [PATCH] Add 404 and 500 page --- src/lib.rs | 11 +++++++++++ src/state/error.rs | 38 ++++++++++++++++++++++++++++++-------- templates/404.html | 8 ++++++++ templates/error.html | 13 ++++++++++++- templates/users/edit.html | 2 +- 5 files changed, 62 insertions(+), 10 deletions(-) create mode 100644 templates/404.html diff --git a/src/lib.rs b/src/lib.rs index dd7479d..73e6cac 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,5 @@ +use askama::Template; +use askama_web::WebTemplate; use axum::{ Router, routing::{get, post}, @@ -29,5 +31,14 @@ pub fn build_app(state: AppState) -> Router { .route("/users", post(routes::user::create)) .route("/users/{id}/delete", post(routes::user::delete)) .nest("/assets", static_router()) + .fallback(error_handler) .with_state(state) } + +#[derive(Template, WebTemplate)] +#[template(path = "404.html")] +struct NotFoundTemplate {} + +pub async fn error_handler() -> impl axum::response::IntoResponse { + NotFoundTemplate {} +} diff --git a/src/state/error.rs b/src/state/error.rs index 36dedc1..9a316be 100644 --- a/src/state/error.rs +++ b/src/state/error.rs @@ -1,17 +1,17 @@ use askama::Template; use askama_web::WebTemplate; use axum::response::{IntoResponse, Response}; +use log::error; use snafu::prelude::*; use crate::{ - models::{book::BookError, user::UserError}, + models::{ + book::{BookError, NotFoundSnafu}, + user::UserError, + }, state::config::ConfigError, }; -#[derive(Template, WebTemplate)] -#[template(path = "error.html")] -struct ErrorTemplate {} - #[derive(Snafu, Debug)] #[snafu(visibility(pub))] pub enum AppStateError { @@ -38,8 +38,30 @@ pub enum AppStateError { }, } -impl IntoResponse for AppStateError { - fn into_response(self) -> Response { - ErrorTemplate {}.into_response() +#[derive(Template, WebTemplate)] +#[template(path = "error.html")] +struct ErrorTemplate { + state: AppStateErrorContext, +} + +struct AppStateErrorContext { + pub errors: Vec, +} + +impl From for AppStateErrorContext { + fn from(e: AppStateError) -> Self { + error!("{:?}", e); + + Self { errors: vec![e] } + } +} + +impl IntoResponse for AppStateError { + fn into_response(self) -> Response { + let error_context = AppStateErrorContext::from(self); + ErrorTemplate { + state: error_context, + } + .into_response() } } diff --git a/templates/404.html b/templates/404.html new file mode 100644 index 0000000..854fcc8 --- /dev/null +++ b/templates/404.html @@ -0,0 +1,8 @@ +{% extends "base.html" %} +{% block main %} +
+

Oops ! This page does not exist

+

404 NOT FOUND

+ Back to home +
+{% endblock %} diff --git a/templates/error.html b/templates/error.html index 59fd4d6..e84c368 100644 --- a/templates/error.html +++ b/templates/error.html @@ -1 +1,12 @@ -Error +{% extends "base.html" %} +{% block main %} +
+

Oops ! An Error occured

+
+ {% for error in state.errors %} +

{{ error }}

+ {% endfor %} +
+ Back to home +
+{% endblock %} diff --git a/templates/users/edit.html b/templates/users/edit.html index 3b87fec..98d6669 100644 --- a/templates/users/edit.html +++ b/templates/users/edit.html @@ -14,7 +14,7 @@ o{% import "components/inputs.html" as form_helpers %}
- +