use std::net::AddrParseError;
use thiserror::Error;
#[derive(Error, Debug)]
pub enum ProxyError {
#[error("Configuration error: {0}")]
Config(String),
#[error("Backend error: {0}")]
Backend(String),
#[error("Parse error: {0}")]
Parse(String),
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),
#[error("HTTP error: {0}")]
Http(String),
#[error("Connection error: {0}")]
Connection(String),
#[error("Directive error: {0}")]
Directive(String),
#[error("Invalid URL: {0}")]
InvalidUrl(String),
#[error("Invalid address: {0}")]
Address(String),
}
pub type Result<T> = std::result::Result<T, ProxyError>;
impl From<AddrParseError> for ProxyError {
fn from(err: AddrParseError) -> Self {
ProxyError::Address(err.to_string())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_display() {
let err = ProxyError::Config("Test error".to_string());
assert_eq!(err.to_string(), "Configuration error: Test error");
}
#[test]
fn test_io_error_conversion() {
let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
let proxy_err: ProxyError = io_err.into();
assert!(matches!(proxy_err, ProxyError::Io(_)));
assert!(proxy_err.to_string().contains("file not found"));
}
#[test]
fn test_result_type_alias() {
let result: Result<String> = Ok("test".to_string());
assert!(result.is_ok());
let result: Result<String> = Err(ProxyError::Config("error".to_string()));
assert!(result.is_err());
}
}