1use std::net::AddrParseError;
7use thiserror::Error;
8
9#[derive(Error, Debug)]
11pub enum ProxyError {
12 #[error("Configuration error: {0}")]
14 Config(String),
15
16 #[error("Backend error: {0}")]
18 Backend(String),
19
20 #[error("Parse error: {0}")]
22 Parse(String),
23
24 #[error("I/O error: {0}")]
26 Io(#[from] std::io::Error),
27
28 #[error("HTTP error: {0}")]
30 Http(String),
31
32 #[error("Connection error: {0}")]
34 Connection(String),
35
36 #[error("Directive error: {0}")]
38 Directive(String),
39
40 #[error("Invalid URL: {0}")]
42 InvalidUrl(String),
43
44 #[error("Invalid address: {0}")]
46 Address(String),
47}
48
49pub type Result<T> = std::result::Result<T, ProxyError>;
53
54impl From<AddrParseError> for ProxyError {
55 fn from(err: AddrParseError) -> Self {
56 ProxyError::Address(err.to_string())
57 }
58}
59
60#[cfg(test)]
61mod tests {
62 use super::*;
63
64 #[test]
65 fn test_error_display() {
66 let err = ProxyError::Config("Test error".to_string());
67 assert_eq!(err.to_string(), "Configuration error: Test error");
68 }
69
70 #[test]
71 fn test_io_error_conversion() {
72 let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
73 let proxy_err: ProxyError = io_err.into();
74 assert!(matches!(proxy_err, ProxyError::Io(_)));
75 assert!(proxy_err.to_string().contains("file not found"));
76 }
77
78 #[test]
79 fn test_result_type_alias() {
80 let result: Result<String> = Ok("test".to_string());
81 assert!(result.is_ok());
82
83 let result: Result<String> = Err(ProxyError::Config("error".to_string()));
84 assert!(result.is_err());
85 }
86}