Skip to main content

rustgate/
error.rs

1use std::fmt;
2
3#[derive(Debug)]
4pub enum ProxyError {
5    Io(std::io::Error),
6    Hyper(hyper::Error),
7    Http(http::Error),
8    Tls(rustls::Error),
9    Rcgen(rcgen::Error),
10    AddrParse(std::net::AddrParseError),
11    WebSocket(String),
12    Serde(serde_json::Error),
13    Protocol(String),
14    Other(String),
15}
16
17impl fmt::Display for ProxyError {
18    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19        match self {
20            ProxyError::Io(e) => write!(f, "IO error: {e}"),
21            ProxyError::Hyper(e) => write!(f, "Hyper error: {e}"),
22            ProxyError::Http(e) => write!(f, "HTTP error: {e}"),
23            ProxyError::Tls(e) => write!(f, "TLS error: {e}"),
24            ProxyError::Rcgen(e) => write!(f, "Certificate error: {e}"),
25            ProxyError::AddrParse(e) => write!(f, "Address parse error: {e}"),
26            ProxyError::WebSocket(e) => write!(f, "WebSocket error: {e}"),
27            ProxyError::Serde(e) => write!(f, "Serialization error: {e}"),
28            ProxyError::Protocol(e) => write!(f, "Protocol error: {e}"),
29            ProxyError::Other(e) => write!(f, "{e}"),
30        }
31    }
32}
33
34impl std::error::Error for ProxyError {}
35
36impl From<std::io::Error> for ProxyError {
37    fn from(e: std::io::Error) -> Self {
38        ProxyError::Io(e)
39    }
40}
41
42impl From<hyper::Error> for ProxyError {
43    fn from(e: hyper::Error) -> Self {
44        ProxyError::Hyper(e)
45    }
46}
47
48impl From<http::Error> for ProxyError {
49    fn from(e: http::Error) -> Self {
50        ProxyError::Http(e)
51    }
52}
53
54impl From<rustls::Error> for ProxyError {
55    fn from(e: rustls::Error) -> Self {
56        ProxyError::Tls(e)
57    }
58}
59
60impl From<rcgen::Error> for ProxyError {
61    fn from(e: rcgen::Error) -> Self {
62        ProxyError::Rcgen(e)
63    }
64}
65
66impl From<std::net::AddrParseError> for ProxyError {
67    fn from(e: std::net::AddrParseError) -> Self {
68        ProxyError::AddrParse(e)
69    }
70}
71
72impl From<serde_json::Error> for ProxyError {
73    fn from(e: serde_json::Error) -> Self {
74        ProxyError::Serde(e)
75    }
76}
77
78impl From<tokio_tungstenite::tungstenite::Error> for ProxyError {
79    fn from(e: tokio_tungstenite::tungstenite::Error) -> Self {
80        ProxyError::WebSocket(e.to_string())
81    }
82}
83
84pub type Result<T> = std::result::Result<T, ProxyError>;