use actix_web::{Error, HttpResponse, web}; use tera::{Context, Tera}; pub struct ErrorController; impl ErrorController { /// Renders a 404 Not Found page with customizable content pub async fn not_found( tmpl: web::Data, error_title: Option<&str>, error_message: Option<&str>, return_url: Option<&str>, return_text: Option<&str>, ) -> Result { let mut context = Context::new(); // Set default or custom error content context.insert("error_title", &error_title.unwrap_or("Page Not Found")); context.insert( "error_message", &error_message .unwrap_or("The page you're looking for doesn't exist or has been moved."), ); // Optional return URL and text if let Some(url) = return_url { context.insert("return_url", &url); context.insert("return_text", &return_text.unwrap_or("Return")); } // Render the 404 template with 404 status match tmpl.render("errors/404.html", &context) { Ok(rendered) => Ok(HttpResponse::NotFound() .content_type("text/html; charset=utf-8") .body(rendered)), Err(e) => { log::error!("Failed to render 404 template: {}", e); // Fallback to simple text response Ok(HttpResponse::NotFound() .content_type("text/plain") .body("404 - Page Not Found")) } } } /// Renders a 404 page for contract not found pub async fn contract_not_found( tmpl: web::Data, contract_id: Option<&str>, ) -> Result { let error_title = "Contract Not Found"; let error_message = if let Some(id) = contract_id { format!( "The contract with ID '{}' doesn't exist or has been removed.", id ) } else { "The contract you're looking for doesn't exist or has been removed.".to_string() }; Self::not_found( tmpl, Some(error_title), Some(&error_message), Some("/contracts"), Some("Back to Contracts"), ) .await } // calendar_event_not_found removed - not used /// Renders a 404 page for company not found pub async fn company_not_found( tmpl: web::Data, company_id: Option<&str>, ) -> Result { let error_title = "Company Not Found"; let error_message = if let Some(id) = company_id { format!( "The company with ID '{}' doesn't exist or has been removed.", id ) } else { "The company you're looking for doesn't exist or has been removed.".to_string() }; Self::not_found( tmpl, Some(error_title), Some(&error_message), Some("/company"), Some("Back to Companies"), ) .await } /// Renders a generic 404 page pub async fn generic_not_found(tmpl: web::Data) -> Result { Self::not_found(tmpl, None, None, None, None).await } } /// Helper function to quickly render a contract not found response pub async fn render_contract_not_found( tmpl: &web::Data, contract_id: Option<&str>, ) -> Result { ErrorController::contract_not_found(tmpl.clone(), contract_id).await } // render_calendar_event_not_found removed - not used /// Helper function to quickly render a company not found response pub async fn render_company_not_found( tmpl: &web::Data, company_id: Option<&str>, ) -> Result { ErrorController::company_not_found(tmpl.clone(), company_id).await } /// Helper function to quickly render a generic not found response pub async fn render_generic_not_found(tmpl: web::Data) -> Result { ErrorController::generic_not_found(tmpl).await }