use axum::{ http::StatusCode, response::{Html, IntoResponse}, }; pub struct AppError { code: StatusCode, message: String, user_message: String, } impl AppError { pub fn new(message: impl Into) -> Self { Self { message: message.into(), user_message: "".to_owned(), code: StatusCode::INTERNAL_SERVER_ERROR, } } pub fn with_user_message(self, user_message: impl Into) -> Self { Self { user_message: user_message.into(), ..self } } // pub fn with_code(self, code: StatusCode) -> Self { // Self { // code, // ..self // } // } } impl IntoResponse for AppError { fn into_response(self) -> axum::response::Response { println!("AppError: {}", self.message); ( self.code, Html(format!( r#" Oops!

Oops!

Sorry, but something went wrong.

{}

"#, self.user_message )), ) .into_response() } } impl From for AppError { fn from(err: askama_axum::Error) -> Self { AppError::new(format!("Template error: {:#}", err)) } } impl From for AppError { fn from(err: dotenvy::Error) -> Self { AppError::new(format!("Dotenv error: {:#}", err)) } } impl From for AppError { fn from(err: sqlx::Error) -> Self { AppError::new(format!("Database query error: {:#}", err)) } } impl From for AppError { fn from(err: String) -> Self { AppError::new(err) } } impl From<&str> for AppError { fn from(err: &str) -> Self { AppError::new(err) } }