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
use crate::frame::Frame;
use std::io;
use std::string::FromUtf8Error;
use thiserror::Error;
use tokio::sync::mpsc::error::SendError;
use tokio::time::error::Elapsed;

#[derive(Error, Debug)]
pub enum CloseError {
    #[error("{source}")]
    SendError {
        #[from]
        source: SendError<Frame>,
    },

    #[error("{source}")]
    Timeout {
        #[from]
        source: Elapsed,
    },
}

#[derive(Error, Debug)]
pub enum HandshakeError {
    #[error("Couldn't find Sec-WebSocket-Key header in the request")]
    NoSecWebsocketKey,

    #[error("IO Error happened: {source}")]
    IOError {
        #[from]
        source: io::Error,
    },

    #[error("{source}")]
    FromUtf8Error {
        #[from]
        source: FromUtf8Error,
    },

    #[error("Server didn't upgrade the connection")]
    NoUpgrade,

    #[error("Sever didn't send a valid Sec-WebSocket-Accept key")]
    InvalidAcceptKey,
}

#[derive(Error, Debug)]
pub enum StreamError {
    #[error("{source}")]
    IOError {
        #[from]
        source: io::Error,
    },

    #[error("channel communication error")]
    CommunicationError,

    #[error("{source}")]
    InternalSendError {
        #[from]
        source: SendError<Frame>,
    },

    #[error("{source}")]
    CloseChannelError {
        #[from]
        source: SendError<bool>,
    },

    #[error("{source}")]
    UTF8Error {
        #[from]
        source: FromUtf8Error,
    },

    #[error("Invalid frame while there is a fragmented message in progress")]
    InvalidFrameFragmentation,

    #[error("Incoming fragmented message but there is one already in progress")]
    FragmentedInProgress,

    #[error("Invalid continuation frame: no fragmented message to continue")]
    InvalidContinuationFrame,
}