1use thiserror::Error;
2
3use crate::frame::OpCode;
4
5#[derive(Debug, Error)]
8pub enum WsError {
9 #[error("invalid uri `{0}`")]
11 InvalidUri(String),
12 #[error("unsupported proxy, expect socks5 or http, got {0}")]
13 CertFileNotFound(String),
15 #[error("load cert {0} failed")]
16 LoadCertFailed(String),
18 #[error("connection failed `{0}`")]
19 ConnectionFailed(String),
21 #[error("tls dns lookup failed `{0}`")]
22 TlsDnsFailed(String),
24 #[error("io error {0:?}")]
25 IOError(std::io::Error),
27 #[error("{0}")]
28 HandShakeFailed(String),
30 #[error("{error:?}")]
32 ProtocolError {
33 close_code: u16,
35 error: ProtocolError,
37 },
38 #[error("unsupported frame {0:?}")]
40 UnsupportedFrame(OpCode),
41
42 #[cfg(any(
43 feature = "deflate",
44 feature = "deflate_ng",
45 feature = "deflate_static"
46 ))]
47 #[error("compress failed {0}")]
49 CompressFailed(String),
50 #[cfg(any(
51 feature = "deflate",
52 feature = "deflate_ng",
53 feature = "deflate_static"
54 ))]
55 #[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#[derive(Debug, Error)]
74pub enum ProtocolError {
75 #[error("insufficient data len {0}")]
77 InsufficientLen(usize),
78 #[error("invalid leading bits {0:b}")]
79 InvalidLeadingBits(u8),
81 #[error("invalid opcode {0}")]
83 InvalidOpcode(u8),
84 #[error("invalid leading payload len {0}")]
86 InvalidLeadingLen(u8),
87 #[error("mismatch data len, expect {0}, got {1}")]
89 MisMatchDataLen(usize, usize),
90 #[error("missing init fragmented frame")]
92 MissInitialFragmentedFrame,
93 #[error("not continue frame after init fragmented frame")]
95 NotContinueFrameAfterFragmented,
96 #[error("fragmented control frame ")]
98 FragmentedControlFrame,
99 #[error("control frame is too big {0}")]
101 ControlFrameTooBig(usize),
102 #[error("invalid close frame payload len, expect 0, >= 2")]
104 InvalidCloseFramePayload,
105 #[error("invalid utf-8 text")]
107 InvalidUtf8,
108 #[error("invalid close code {0}")]
110 InvalidCloseCode(u16),
111 #[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 #[error("compressed control frame")]
122 CompressedControlFrame,
123}