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