tokio_websockets/proto/
error.rs

1//! WebSocket protocol error type.
2use std::fmt;
3
4/// Error encountered on protocol violations by the other end of the connection.
5#[allow(clippy::module_name_repetitions)]
6#[derive(Debug)]
7#[non_exhaustive]
8pub enum ProtocolError {
9    /// A fragmented control frame was received.
10    FragmentedControlFrame,
11    /// An invalid close code has been received.
12    InvalidCloseCode,
13    /// An invalid opcode was received.
14    InvalidOpcode,
15    /// An invalid payload length was received.
16    InvalidPayloadLength,
17    /// An invalid RSV was received. This is used by extensions, which are
18    /// currently unsupported.
19    InvalidRsv,
20    /// An invalid UTF-8 segment was received when valid UTF-8 was expected.
21    InvalidUtf8,
22    /// A masked frame was unexpectedly received.
23    UnexpectedMaskedFrame,
24    /// An unmasked frame was unexpectedly received.
25    UnexpectedUnmaskedFrame,
26}
27
28impl ProtocolError {
29    /// Stringify this variant.
30    pub(super) const fn as_str(&self) -> &'static str {
31        match self {
32            ProtocolError::FragmentedControlFrame => "fragmented control frame",
33            ProtocolError::InvalidCloseCode => "invalid close code",
34            ProtocolError::InvalidOpcode => "invalid opcode",
35            ProtocolError::InvalidPayloadLength => "invalid payload length",
36            ProtocolError::InvalidRsv => "invalid extension",
37            ProtocolError::InvalidUtf8 => "invalid utf-8",
38            ProtocolError::UnexpectedMaskedFrame => "unexpected masked frame",
39            ProtocolError::UnexpectedUnmaskedFrame => "unexpected unmasked frame",
40        }
41    }
42}
43
44impl fmt::Display for ProtocolError {
45    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46        f.write_str(self.as_str())
47    }
48}
49
50impl std::error::Error for ProtocolError {}