Skip to main content

async_wsocket/native/
error.rs

1// Copyright (c) 2022-2024 Yuki Kishimoto
2// Distributed under the MIT software license
3
4use core::fmt;
5
6use tokio_tungstenite::tungstenite::Error as WsError;
7use url::ParseError;
8
9#[cfg(feature = "tor")]
10use super::tor;
11
12#[derive(Debug)]
13pub enum Error {
14    /// Ws error
15    Ws(WsError),
16    /// I/O error
17    Io(std::io::Error),
18    /// Socks error
19    #[cfg(feature = "socks")]
20    Socks(tokio_socks::Error),
21    /// Tor error
22    #[cfg(feature = "tor")]
23    Tor(tor::Error),
24    /// Url parse error
25    Url(ParseError),
26    /// Timeout
27    Timeout,
28}
29
30impl std::error::Error for Error {}
31
32impl fmt::Display for Error {
33    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34        match self {
35            Self::Ws(e) => write!(f, "{e}"),
36            Self::Io(e) => write!(f, "{e}"),
37            #[cfg(feature = "socks")]
38            Self::Socks(e) => write!(f, "{e}"),
39            #[cfg(feature = "tor")]
40            Self::Tor(e) => write!(f, "{e}"),
41            Self::Url(e) => write!(f, "{e}"),
42            Self::Timeout => write!(f, "timeout"),
43        }
44    }
45}
46
47impl From<WsError> for Error {
48    fn from(e: WsError) -> Self {
49        Self::Ws(e)
50    }
51}
52
53impl From<std::io::Error> for Error {
54    fn from(e: std::io::Error) -> Self {
55        Self::Io(e)
56    }
57}
58
59#[cfg(feature = "socks")]
60impl From<tokio_socks::Error> for Error {
61    fn from(e: tokio_socks::Error) -> Self {
62        Self::Socks(e)
63    }
64}
65
66#[cfg(feature = "tor")]
67impl From<tor::Error> for Error {
68    fn from(e: tor::Error) -> Self {
69        Self::Tor(e)
70    }
71}
72
73impl Error {
74    #[inline]
75    pub(super) fn empty_host() -> Self {
76        Self::Url(ParseError::EmptyHost)
77    }
78
79    #[inline]
80    pub(super) fn invalid_port() -> Self {
81        Self::Url(ParseError::InvalidPort)
82    }
83}