ws_tool/
errors.rs

1use thiserror::Error;
2
3use crate::frame::OpCode;
4
5// TODO add custom error kind
6/// errors during handshake, read/write frame
7#[derive(Debug, Error)]
8pub enum WsError {
9    /// invalid websocket connection url
10    #[error("invalid uri `{0}`")]
11    InvalidUri(String),
12    #[error("unsupported proxy, expect socks5 or http, got {0}")]
13    /// invalid cert file path
14    CertFileNotFound(String),
15    #[error("load cert {0} failed")]
16    /// broken certs
17    LoadCertFailed(String),
18    #[error("connection failed `{0}`")]
19    /// failed to connect websocket server
20    ConnectionFailed(String),
21    #[error("tls dns lookup failed `{0}`")]
22    /// tls session DNS is broken
23    TlsDnsFailed(String),
24    #[error("io error {0:?}")]
25    /// raised by underlying stream
26    IOError(std::io::Error),
27    #[error("{0}")]
28    /// invalid protocol handshake
29    HandShakeFailed(String),
30    /// websocket protocol handshake
31    #[error("{error:?}")]
32    ProtocolError {
33        /// peer close code
34        close_code: u16,
35        /// detail error
36        error: ProtocolError,
37    },
38    /// peer send a frame with unknown opcode
39    #[error("unsupported frame {0:?}")]
40    UnsupportedFrame(OpCode),
41
42    #[cfg(any(
43        feature = "deflate",
44        feature = "deflate_ng",
45        feature = "deflate_static"
46    ))]
47    /// compress failed
48    #[error("compress failed {0}")]
49    CompressFailed(String),
50    #[cfg(any(
51        feature = "deflate",
52        feature = "deflate_ng",
53        feature = "deflate_static"
54    ))]
55    /// decompress failed
56    #[error("decompress failed {0}")]
57    DeCompressFailed(String),
58}
59
60impl From<std::io::Error> for WsError {
61    fn from(e: std::io::Error) -> Self {
62        WsError::IOError(e)
63    }
64}
65
66impl From<WsError> for std::io::Error {
67    fn from(e: WsError) -> Self {
68        std::io::Error::new(std::io::ErrorKind::InvalidData, e)
69    }
70}
71
72/// errors during decode frame from bytes
73#[derive(Debug, Error)]
74pub enum ProtocolError {
75    /// need more data to construct valid frame
76    #[error("insufficient data len {0}")]
77    InsufficientLen(usize),
78    #[error("invalid leading bits {0:b}")]
79    /// need more data to construct valid frame header
80    InvalidLeadingBits(u8),
81    /// invalid frame opcode
82    #[error("invalid opcode {0}")]
83    InvalidOpcode(u8),
84    /// invalid bit of fin, rsv1, rsv2, rsv3
85    #[error("invalid leading payload len {0}")]
86    InvalidLeadingLen(u8),
87    /// mismatch payload len in frame header and actual payload
88    #[error("mismatch data len, expect {0}, got {1}")]
89    MisMatchDataLen(usize, usize),
90    /// missing first frame in fragmented frame
91    #[error("missing init fragmented frame")]
92    MissInitialFragmentedFrame,
93    /// invalid data frame after first fragmented frame
94    #[error("not continue frame after init fragmented frame")]
95    NotContinueFrameAfterFragmented,
96    /// control framed should not be fragmented
97    #[error("fragmented control frame ")]
98    FragmentedControlFrame,
99    /// control frame is too big
100    #[error("control frame is too big {0}")]
101    ControlFrameTooBig(usize),
102    /// invalid payload content(need to be valid utf8) of a close frame
103    #[error("invalid close frame payload len, expect 0, >= 2")]
104    InvalidCloseFramePayload,
105    /// invalid utf8 payload of a text frame
106    #[error("invalid utf-8 text")]
107    InvalidUtf8,
108    /// invalid close code
109    #[error("invalid close code {0}")]
110    InvalidCloseCode(u16),
111    /// payload exceed payload len limit
112    #[error("payload too large, max payload size {0}")]
113    PayloadTooLarge(usize),
114
115    #[cfg(any(
116        feature = "deflate",
117        feature = "deflate_ng",
118        feature = "deflate_static"
119    ))]
120    /// compressed control frame
121    #[error("compressed control frame")]
122    CompressedControlFrame,
123}