use thiserror::Error;
pub type HttpResult<T> = Result<T, HttpError>;
#[derive(Error, Debug)]
pub enum HttpError {
#[error("Server startup failed: {message}")]
StartupFailed { message: String },
#[error("Server shutdown failed: {message}")]
ShutdownFailed { message: String },
#[error("Configuration error: {message}")]
ConfigError { message: String },
#[error("Service resolution failed: {service}")]
ServiceResolutionFailed { service: String },
#[error("Request timeout")]
RequestTimeout,
#[error("Request too large: {size} bytes exceeds limit of {limit} bytes")]
RequestTooLarge { size: usize, limit: usize },
#[error("Invalid request: {message}")]
BadRequest { message: String },
#[error("Internal server error: {message}")]
InternalError { message: String },
#[error("Health check failed: {reason}")]
HealthCheckFailed { reason: String },
#[error("Database error: {message}")]
DatabaseError { message: String },
#[error("Validation error: {message}")]
ValidationError { message: String },
#[error("Resource not found: {resource}")]
NotFound { resource: String },
#[error("Resource already exists: {message}")]
Conflict { message: String },
#[error("Unauthorized access")]
Unauthorized,
#[error("Access forbidden: {message}")]
Forbidden { message: String },
}
impl HttpError {
pub fn startup<T: Into<String>>(message: T) -> Self {
HttpError::StartupFailed {
message: message.into(),
}
}
pub fn shutdown<T: Into<String>>(message: T) -> Self {
HttpError::ShutdownFailed {
message: message.into(),
}
}
pub fn config<T: Into<String>>(message: T) -> Self {
HttpError::ConfigError {
message: message.into(),
}
}
pub fn service_resolution<T: Into<String>>(service: T) -> Self {
HttpError::ServiceResolutionFailed {
service: service.into(),
}
}
pub fn bad_request<T: Into<String>>(message: T) -> Self {
HttpError::BadRequest {
message: message.into(),
}
}
pub fn internal<T: Into<String>>(message: T) -> Self {
HttpError::InternalError {
message: message.into(),
}
}
pub fn health_check<T: Into<String>>(reason: T) -> Self {
HttpError::HealthCheckFailed {
reason: reason.into(),
}
}
pub fn database_error<T: Into<String>>(message: T) -> Self {
HttpError::DatabaseError {
message: message.into(),
}
}
pub fn validation_error<T: Into<String>>(message: T) -> Self {
HttpError::ValidationError {
message: message.into(),
}
}
pub fn not_found<T: Into<String>>(resource: T) -> Self {
HttpError::NotFound {
resource: resource.into(),
}
}
pub fn conflict<T: Into<String>>(message: T) -> Self {
HttpError::Conflict {
message: message.into(),
}
}
pub fn unauthorized() -> Self {
HttpError::Unauthorized
}
pub fn forbidden<T: Into<String>>(message: T) -> Self {
HttpError::Forbidden {
message: message.into(),
}
}
pub fn timeout() -> Self {
HttpError::RequestTimeout
}
pub fn payload_too_large(size: usize, limit: usize) -> Self {
HttpError::RequestTooLarge { size, limit }
}
pub fn with_detail<T: Into<String>>(self, _detail: T) -> Self {
self
}
pub fn error_code(&self) -> &'static str {
match self {
HttpError::StartupFailed { .. } => "SERVER_STARTUP_FAILED",
HttpError::ShutdownFailed { .. } => "SERVER_SHUTDOWN_FAILED",
HttpError::ConfigError { .. } => "CONFIGURATION_ERROR",
HttpError::ServiceResolutionFailed { .. } => "SERVICE_RESOLUTION_FAILED",
HttpError::RequestTimeout => "REQUEST_TIMEOUT",
HttpError::RequestTooLarge { .. } => "REQUEST_TOO_LARGE",
HttpError::BadRequest { .. } => "BAD_REQUEST",
HttpError::InternalError { .. } => "INTERNAL_ERROR",
HttpError::HealthCheckFailed { .. } => "HEALTH_CHECK_FAILED",
HttpError::DatabaseError { .. } => "DATABASE_ERROR",
HttpError::ValidationError { .. } => "VALIDATION_ERROR",
HttpError::NotFound { .. } => "RESOURCE_NOT_FOUND",
HttpError::Conflict { .. } => "RESOURCE_CONFLICT",
HttpError::Unauthorized => "UNAUTHORIZED_ACCESS",
HttpError::Forbidden { .. } => "ACCESS_FORBIDDEN",
}
}
}
impl From<elif_core::ConfigError> for HttpError {
fn from(err: elif_core::ConfigError) -> Self {
HttpError::ConfigError {
message: err.to_string(),
}
}
}
impl From<std::io::Error> for HttpError {
fn from(err: std::io::Error) -> Self {
HttpError::InternalError {
message: format!("IO error: {}", err),
}
}
}
impl From<hyper::Error> for HttpError {
fn from(err: hyper::Error) -> Self {
HttpError::InternalError {
message: format!("Hyper error: {}", err),
}
}
}
impl From<serde_json::Error> for HttpError {
fn from(err: serde_json::Error) -> Self {
HttpError::InternalError {
message: format!("JSON serialization error: {}", err),
}
}
}
#[cfg(feature = "orm")]
impl From<orm::ModelError> for HttpError {
fn from(err: orm::ModelError) -> Self {
match err {
orm::ModelError::NotFound(table) => HttpError::NotFound { resource: table },
orm::ModelError::Validation(msg) => HttpError::ValidationError { message: msg },
orm::ModelError::Database(msg) => HttpError::DatabaseError { message: msg },
orm::ModelError::Connection(msg) => HttpError::DatabaseError {
message: format!("Connection error: {}", msg),
},
orm::ModelError::Transaction(msg) => HttpError::DatabaseError {
message: format!("Transaction error: {}", msg),
},
orm::ModelError::Query(msg) => HttpError::BadRequest {
message: format!("Query error: {}", msg),
},
orm::ModelError::Schema(msg) => HttpError::InternalError {
message: format!("Schema error: {}", msg),
},
orm::ModelError::Migration(msg) => HttpError::InternalError {
message: format!("Migration error: {}", msg),
},
orm::ModelError::MissingPrimaryKey => HttpError::BadRequest {
message: "Missing or invalid primary key".to_string(),
},
orm::ModelError::Relationship(msg) => HttpError::BadRequest {
message: format!("Relationship error: {}", msg),
},
orm::ModelError::Serialization(msg) => HttpError::InternalError {
message: format!("Serialization error: {}", msg),
},
orm::ModelError::Event(msg) => HttpError::InternalError {
message: format!("Event error: {}", msg),
},
}
}
}
#[cfg(feature = "orm")]
impl From<orm::QueryError> for HttpError {
fn from(err: orm::QueryError) -> Self {
match err {
orm::QueryError::InvalidSql(msg) => HttpError::BadRequest {
message: format!("Invalid SQL query: {}", msg),
},
orm::QueryError::MissingFields(msg) => HttpError::BadRequest {
message: format!("Missing required fields: {}", msg),
},
orm::QueryError::InvalidParameter(msg) => HttpError::BadRequest {
message: format!("Invalid query parameter: {}", msg),
},
orm::QueryError::UnsupportedOperation(msg) => HttpError::BadRequest {
message: format!("Unsupported operation: {}", msg),
},
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_creation() {
let error = HttpError::startup("Failed to bind to port");
assert!(matches!(error, HttpError::StartupFailed { .. }));
assert_eq!(error.error_code(), "SERVER_STARTUP_FAILED");
}
#[test]
fn test_error_codes() {
assert_eq!(HttpError::bad_request("test").error_code(), "BAD_REQUEST");
assert_eq!(HttpError::RequestTimeout.error_code(), "REQUEST_TIMEOUT");
assert_eq!(HttpError::internal("test").error_code(), "INTERNAL_ERROR");
}
#[test]
fn test_config_error_conversion() {
let config_error = elif_core::ConfigError::validation_failed("Test validation error");
let http_error = HttpError::from(config_error);
assert!(matches!(http_error, HttpError::ConfigError { .. }));
}
#[test]
fn test_io_error_conversion() {
let io_error = std::io::Error::new(std::io::ErrorKind::PermissionDenied, "Access denied");
let http_error = HttpError::from(io_error);
assert!(matches!(http_error, HttpError::InternalError { .. }));
}
#[test]
fn test_validation_error_creation() {
let validation_error = HttpError::validation_error("Field is required");
assert_eq!(validation_error.error_code(), "VALIDATION_ERROR");
}
}