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    /// Socks error
17    #[cfg(feature = "socks")]
18    Socks(tokio_socks::Error),
19    /// Tor error
20    #[cfg(feature = "tor")]
21    Tor(tor::Error),
22    /// Url parse error
23    Url(ParseError),
24    /// Timeout
25    Timeout,
26}
27
28impl std::error::Error for Error {}
29
30impl fmt::Display for Error {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        match self {
33            Self::Ws(e) => write!(f, "{e}"),
34            #[cfg(feature = "socks")]
35            Self::Socks(e) => write!(f, "{e}"),
36            #[cfg(feature = "tor")]
37            Self::Tor(e) => write!(f, "{e}"),
38            Self::Url(e) => write!(f, "{e}"),
39            Self::Timeout => write!(f, "timeout"),
40        }
41    }
42}
43
44impl From<WsError> for Error {
45    fn from(e: WsError) -> Self {
46        Self::Ws(e)
47    }
48}
49
50#[cfg(feature = "socks")]
51impl From<tokio_socks::Error> for Error {
52    fn from(e: tokio_socks::Error) -> Self {
53        Self::Socks(e)
54    }
55}
56
57#[cfg(feature = "tor")]
58impl From<tor::Error> for Error {
59    fn from(e: tor::Error) -> Self {
60        Self::Tor(e)
61    }
62}
63
64impl Error {
65    #[inline]
66    #[cfg(any(feature = "socks", feature = "tor"))]
67    pub(super) fn empty_host() -> Self {
68        Self::Url(ParseError::EmptyHost)
69    }
70
71    #[inline]
72    #[cfg(any(feature = "socks", feature = "tor"))]
73    pub(super) fn invalid_port() -> Self {
74        Self::Url(ParseError::InvalidPort)
75    }
76}