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
use std;
use std::convert::From;
use std::error::Error;
use std::fmt;
use std::io;
use std::str::Utf8Error;
pub type WebSocketResult<T> = Result<T, WebSocketError>;
#[cfg(feature = "async")]
pub mod r#async {
use super::WebSocketError;
use futures::Future;
pub type WebSocketFuture<I> = Box<dyn Future<Item = I, Error = WebSocketError> + Send>;
}
#[derive(Debug)]
pub enum WebSocketError {
ProtocolError(&'static str),
DataFrameError(&'static str),
NoDataAvailable,
IoError(io::Error),
Utf8Error(Utf8Error),
Other(Box<dyn std::error::Error + Send + Sync + 'static>),
}
impl fmt::Display for WebSocketError {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.write_str("WebSocketError: ")?;
match self {
WebSocketError::ProtocolError(_) => fmt.write_str("WebSocket protocol error"),
WebSocketError::DataFrameError(_) => fmt.write_str("WebSocket data frame error"),
WebSocketError::NoDataAvailable => fmt.write_str("No data available"),
WebSocketError::IoError(_) => fmt.write_str("I/O failure"),
WebSocketError::Utf8Error(_) => fmt.write_str("UTF-8 failure"),
WebSocketError::Other(x) => x.fmt(fmt),
}
}
}
impl Error for WebSocketError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match *self {
WebSocketError::IoError(ref error) => Some(error),
WebSocketError::Utf8Error(ref error) => Some(error),
WebSocketError::Other(ref error) => error.source(),
_ => 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<Utf8Error> for WebSocketError {
fn from(err: Utf8Error) -> WebSocketError {
WebSocketError::Utf8Error(err)
}
}