hostbasket/actix_mvc_app/src/controllers/error.rs
Mahmoud-Emad d3a66d4fc8 feat: Add initial production deployment support
- Add .env.example file for environment variable setup
- Add .gitignore to manage sensitive files and directories
- Add Dockerfile.prod for production-ready Docker image
- Add PRODUCTION_CHECKLIST.md for pre/post deployment steps
- Add PRODUCTION_DEPLOYMENT.md for deployment instructions
- Add STRIPE_SETUP.md for Stripe payment configuration
- Add config/default.toml for default configuration settings
- Add config/local.toml.example for local configuration template
2025-06-25 18:32:20 +03:00

126 lines
4.1 KiB
Rust

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<Tera>,
error_title: Option<&str>,
error_message: Option<&str>,
return_url: Option<&str>,
return_text: Option<&str>,
) -> Result<HttpResponse, Error> {
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<Tera>,
contract_id: Option<&str>,
) -> Result<HttpResponse, Error> {
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<Tera>,
company_id: Option<&str>,
) -> Result<HttpResponse, Error> {
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<Tera>) -> Result<HttpResponse, Error> {
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<Tera>,
contract_id: Option<&str>,
) -> Result<HttpResponse, Error> {
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<Tera>,
company_id: Option<&str>,
) -> Result<HttpResponse, Error> {
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<Tera>) -> Result<HttpResponse, Error> {
ErrorController::generic_not_found(tmpl).await
}