bookforge/src/state/error.rs

77 lines
1.7 KiB
Rust
Raw Normal View History

2026-01-23 13:56:30 +01:00
use askama::Template;
use askama_web::WebTemplate;
use axum::response::{IntoResponse, Response};
2026-01-30 10:33:25 +01:00
use log::error;
2026-01-26 01:05:10 +01:00
use snafu::prelude::*;
2026-01-28 00:38:24 +01:00
use crate::{
2026-01-30 11:22:32 +01:00
models::{book::BookError, user::UserError},
2026-01-31 00:05:53 +01:00
routes::template_ctx::TemplateCtx,
2026-01-28 00:38:24 +01:00
state::config::ConfigError,
};
2026-01-23 13:56:30 +01:00
2026-01-26 01:05:10 +01:00
#[derive(Snafu, Debug)]
#[snafu(visibility(pub))]
2026-01-23 13:56:30 +01:00
pub enum AppStateError {
Error,
2026-01-26 01:05:10 +01:00
#[snafu(display("Sqlite Error"))]
Sqlite {
source: sea_orm::error::DbErr,
},
#[snafu(display("Config Error"))]
ConfigError {
source: ConfigError,
},
2026-01-27 01:33:21 +01:00
#[snafu(display("Migration Error"))]
Migration {
source: sea_orm::error::DbErr,
},
#[snafu(display("User Model Error"))]
User {
source: UserError,
},
2026-01-28 00:38:24 +01:00
#[snafu(display("Book Model Error"))]
Book {
source: BookError,
},
2026-01-30 17:06:59 +01:00
#[snafu(display("CSV Error"))]
CSV {
source: csv::Error,
},
#[snafu(display("IO Error"))]
IO {
source: std::io::Error,
},
2026-01-23 13:56:30 +01:00
}
2026-01-30 10:33:25 +01:00
#[derive(Template, WebTemplate)]
#[template(path = "error.html")]
struct ErrorTemplate {
state: AppStateErrorContext,
2026-01-31 00:05:53 +01:00
ctx: TemplateCtx,
2026-01-30 10:33:25 +01:00
}
struct AppStateErrorContext {
pub errors: Vec<AppStateError>,
}
impl From<AppStateError> for AppStateErrorContext {
fn from(e: AppStateError) -> Self {
error!("{:?}", e);
Self { errors: vec![e] }
}
}
2026-01-23 13:56:30 +01:00
impl IntoResponse for AppStateError {
fn into_response(self) -> Response {
2026-01-30 10:33:25 +01:00
let error_context = AppStateErrorContext::from(self);
ErrorTemplate {
state: error_context,
2026-01-31 00:05:53 +01:00
ctx: TemplateCtx {
base_path: "".to_string(),
},
2026-01-30 10:33:25 +01:00
}
.into_response()
2026-01-23 13:56:30 +01:00
}
}