1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
use std::io;
use std::str::Utf8Error;
use std::error::Error;
use std::convert::From;
use std::fmt;
use hyper::Error as HttpError;
use url::ParseError;
#[cfg(feature="ssl")]
use openssl::error::ErrorStack as SslError;
#[cfg(feature="ssl")]
use openssl::ssl::HandshakeError as SslHandshakeError;
pub type WebSocketResult<T> = Result<T, WebSocketError>;
#[derive(Debug)]
pub enum WebSocketError {
ProtocolError(&'static str),
RequestError(&'static str),
ResponseError(&'static str),
DataFrameError(&'static str),
NoDataAvailable,
IoError(io::Error),
HttpError(HttpError),
UrlError(ParseError),
WebSocketUrlError(WSUrlErrorKind),
#[cfg(feature="ssl")]
SslError(SslError),
#[cfg(feature="ssl")]
SslHandshakeFailure,
#[cfg(feature="ssl")]
SslHandshakeInterruption,
Utf8Error(Utf8Error),
}
impl fmt::Display for WebSocketError {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
try!(fmt.write_str("WebSocketError: "));
try!(fmt.write_str(self.description()));
Ok(())
}
}
impl Error for WebSocketError {
fn description(&self) -> &str {
match *self {
WebSocketError::ProtocolError(_) => "WebSocket protocol error",
WebSocketError::RequestError(_) => "WebSocket request error",
WebSocketError::ResponseError(_) => "WebSocket response error",
WebSocketError::DataFrameError(_) => "WebSocket data frame error",
WebSocketError::NoDataAvailable => "No data available",
WebSocketError::IoError(_) => "I/O failure",
WebSocketError::HttpError(_) => "HTTP failure",
WebSocketError::UrlError(_) => "URL failure",
#[cfg(feature="ssl")]
WebSocketError::SslError(_) => "SSL failure",
#[cfg(feature="ssl")]
WebSocketError::SslHandshakeFailure => "SSL Handshake failure",
#[cfg(feature="ssl")]
WebSocketError::SslHandshakeInterruption => "SSL Handshake interrupted",
WebSocketError::Utf8Error(_) => "UTF-8 failure",
WebSocketError::WebSocketUrlError(_) => "WebSocket URL failure",
}
}
fn cause(&self) -> Option<&Error> {
match *self {
WebSocketError::IoError(ref error) => Some(error),
WebSocketError::HttpError(ref error) => Some(error),
WebSocketError::UrlError(ref error) => Some(error),
#[cfg(feature="ssl")]
WebSocketError::SslError(ref error) => Some(error),
WebSocketError::Utf8Error(ref error) => Some(error),
WebSocketError::WebSocketUrlError(ref error) => Some(error),
_ => None,
}
}
}
impl From<io::Error> for WebSocketError {
fn from(err: io::Error) -> WebSocketError {
if err.kind() == io::ErrorKind::UnexpectedEof {
return WebSocketError::NoDataAvailable;
}
WebSocketError::IoError(err)
}
}
impl From<HttpError> for WebSocketError {
fn from(err: HttpError) -> WebSocketError {
WebSocketError::HttpError(err)
}
}
impl From<ParseError> for WebSocketError {
fn from(err: ParseError) -> WebSocketError {
WebSocketError::UrlError(err)
}
}
#[cfg(feature="ssl")]
impl From<SslError> for WebSocketError {
fn from(err: SslError) -> WebSocketError {
WebSocketError::SslError(err)
}
}
#[cfg(feature="ssl")]
impl<T> From<SslHandshakeError<T>> for WebSocketError {
fn from(err: SslHandshakeError<T>) -> WebSocketError {
match err {
SslHandshakeError::SetupFailure(err) => WebSocketError::SslError(err),
SslHandshakeError::Failure(_) => WebSocketError::SslHandshakeFailure,
SslHandshakeError::Interrupted(_) => WebSocketError::SslHandshakeInterruption,
}
}
}
impl From<Utf8Error> for WebSocketError {
fn from(err: Utf8Error) -> WebSocketError {
WebSocketError::Utf8Error(err)
}
}
impl From<WSUrlErrorKind> for WebSocketError {
fn from(err: WSUrlErrorKind) -> WebSocketError {
WebSocketError::WebSocketUrlError(err)
}
}
#[derive(Debug)]
pub enum WSUrlErrorKind {
CannotSetFragment,
InvalidScheme,
NoHostName,
}
impl fmt::Display for WSUrlErrorKind {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
try!(fmt.write_str("WebSocket Url Error: "));
try!(fmt.write_str(self.description()));
Ok(())
}
}
impl Error for WSUrlErrorKind {
fn description(&self) -> &str {
match *self {
WSUrlErrorKind::CannotSetFragment => "WebSocket URL cannot set fragment",
WSUrlErrorKind::InvalidScheme => "WebSocket URL invalid scheme",
WSUrlErrorKind::NoHostName => "WebSocket URL no host name provided",
}
}
}