Add 404 and 500 page

This commit is contained in:
gabatxo1312 2026-01-30 10:33:25 +01:00
commit 09a7bdfc76
No known key found for this signature in database
5 changed files with 60 additions and 8 deletions

View file

@ -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 {}
}

View file

@ -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<AppStateError>,
}
impl From<AppStateError> 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()
}
}