bookforge/src/state/error.rs
2026-01-30 11:22:32 +01:00

64 lines
1.4 KiB
Rust

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},
state::config::ConfigError,
};
#[derive(Snafu, Debug)]
#[snafu(visibility(pub))]
pub enum AppStateError {
Error,
#[snafu(display("Sqlite Error"))]
Sqlite {
source: sea_orm::error::DbErr,
},
#[snafu(display("Config Error"))]
ConfigError {
source: ConfigError,
},
#[snafu(display("Migration Error"))]
Migration {
source: sea_orm::error::DbErr,
},
#[snafu(display("User Model Error"))]
User {
source: UserError,
},
#[snafu(display("Book Model Error"))]
Book {
source: BookError,
},
}
#[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()
}
}