cataclysm_ws/
error.rs

1/// Error indicating a frame parsing error
2#[derive(Debug)]
3pub enum FrameParseError {
4    /// Indicates that the first 4 bits of the message are unsupported
5    WrongFinRSV,
6    /// Indicates that the message content is incomplete
7    Incomplete {
8        expected: usize,
9        obtained: usize
10    },
11    /// Indicates that the message is malformed
12    Malformed,
13    /// Indicates that the message contains 0 bytes
14    NullContent,
15    /// The text sent through the message is not valid a utf-8
16    InvalidUtf8(std::string::FromUtf8Error),
17    /// Indicates an unsupported operation code contained in the frame
18    UnsupportedOpCode
19}
20
21impl std::fmt::Display for FrameParseError {
22    fn fmt(&self, formatter: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
23        let content = match self {
24            FrameParseError::WrongFinRSV => format!("first 4 bits of the message are malformed"),
25            FrameParseError::Incomplete{expected, obtained} => format!("mismatching payload length in message (expected: {}, obtained: {})", expected, obtained),
26            FrameParseError::Malformed => format!("the message does not have the corret structure or enough bytes"),
27            FrameParseError::NullContent => format!("can't parse because the message has length 0"),
28            FrameParseError::InvalidUtf8(e) => format!("invalid utf8 bytes, {}", e),
29            FrameParseError::UnsupportedOpCode => format!("the op code received is not supported")
30        };
31        write!(formatter, "{}", content)
32    }
33}
34
35impl std::error::Error for FrameParseError {}
36
37/// Errors thrown by this library
38#[derive(Debug)]
39pub enum Error {
40    /// Standard io error
41    Io(std::io::Error),
42    /// Could not parse properly a frame, the detail is contained inside
43    FrameParse(FrameParseError),
44    /// Indicates that the connection was closed abruptly
45    ConnectionReset
46}
47
48impl std::fmt::Display for Error {
49    fn fmt(&self, formatter: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
50        let content = match self {
51            Error::Io(inner_error) => format!("io error: {}", inner_error),
52            Error::FrameParse(fpe) => format!("frame parse error: {}", fpe),
53            Error::ConnectionReset => format!("connection reset by peer")
54        };
55        write!(formatter, "{}", content)
56    }
57}
58
59impl std::error::Error for Error {}