Skip to main content

socks5_impl/
error.rs

1/// The library's error type.
2#[derive(Debug, thiserror::Error)]
3pub enum Error {
4    #[error("{0}")]
5    Io(#[from] std::io::Error),
6
7    #[error("{0}")]
8    FromUtf8(#[from] std::string::FromUtf8Error),
9
10    #[error("Invalid SOCKS version: {0:x}")]
11    InvalidVersion(u8),
12    #[error("Invalid command: {0:x}")]
13    InvalidCommand(u8),
14    #[error("Invalid address type: {0:x}")]
15    InvalidAtyp(u8),
16    #[error("Invalid reserved bytes: {0:x}")]
17    InvalidReserved(u8),
18    #[error("Invalid authentication status: {0:x}")]
19    InvalidAuthStatus(u8),
20    #[error("Invalid authentication version of subnegotiation: {0:x}")]
21    InvalidAuthSubnegotiation(u8),
22    #[error("Invalid fragment id: {0:x}")]
23    InvalidFragmentId(u8),
24
25    #[error("Invalid authentication method: {0:?}")]
26    #[cfg(feature = "protocol")]
27    InvalidAuthMethod(crate::protocol::AuthMethod),
28
29    #[error("SOCKS version is 4 when 5 is expected")]
30    WrongVersion,
31
32    #[error("AddrParseError: {0}")]
33    AddrParseError(#[from] std::net::AddrParseError),
34
35    #[error("ParseIntError: {0}")]
36    ParseIntError(#[from] std::num::ParseIntError),
37
38    #[error("Utf8Error: {0}")]
39    Utf8Error(#[from] std::str::Utf8Error),
40
41    #[error("Invalid address: {0}")]
42    InvalidAddress(String),
43
44    #[error("Domain name too long: {0}")]
45    DomainTooLong(usize),
46
47    #[error("{0}")]
48    String(String),
49}
50
51impl From<&str> for Error {
52    fn from(s: &str) -> Self {
53        Error::String(s.to_string())
54    }
55}
56
57impl From<String> for Error {
58    fn from(s: String) -> Self {
59        Error::String(s)
60    }
61}
62
63impl From<&String> for Error {
64    fn from(s: &String) -> Self {
65        Error::String(s.to_string())
66    }
67}
68
69impl From<Error> for std::io::Error {
70    fn from(e: Error) -> Self {
71        match e {
72            Error::Io(e) => e,
73            _ => std::io::Error::other(e),
74        }
75    }
76}
77
78#[cfg(feature = "tokio")]
79impl From<tokio::time::error::Elapsed> for Error {
80    fn from(e: tokio::time::error::Elapsed) -> Self {
81        Error::Io(e.into())
82    }
83}
84
85/// The library's `Result` type alias.
86pub type Result<T, E = Error> = std::result::Result<T, E>;