rama_ws/protocol/
error.rs1use crate::protocol::{frame::coding::OpCodeData, message::Message};
2use rama_core::error::{BoxError, ErrorExt};
3use rama_net::conn::is_connection_error;
4use rama_utils::str::utf8;
5use std::{error, fmt, io};
6
7#[derive(Debug)]
9pub enum ProtocolError {
10 Utf8(BoxError),
12 Io(io::Error),
17 InvalidOpcode(u8),
19 InvalidCloseSequence,
21 MessageTooLong {
25 size: usize,
32 max_size: usize,
34 },
35 UnmaskedFrameFromClient,
37 WriteBufferFull(Message),
39 SendAfterClosing,
41 ReceivedAfterClosing,
43 NonZeroReservedBits,
45 MaskedFrameFromServer,
47 FragmentedControlFrame,
49 ControlFrameTooBig,
51 UnknownControlFrameType(u8),
53 ResetWithoutClosingHandshake,
55 UnexpectedContinueFrame,
57 ExpectedFragment(OpCodeData),
59 UnknownDataFrameType(u8),
61 DeflateError(BoxError),
63}
64
65impl ProtocolError {
66 pub fn is_connection_error(&self) -> bool {
69 if let Self::Io(err) = self {
70 is_connection_error(err)
71 } else {
72 false
73 }
74 }
75}
76
77impl From<utf8::DecodeError<'_>> for ProtocolError {
78 fn from(value: utf8::DecodeError<'_>) -> Self {
79 Self::Utf8(BoxError::from(value.to_string()))
80 }
81}
82
83impl From<std::str::Utf8Error> for ProtocolError {
84 fn from(value: std::str::Utf8Error) -> Self {
85 Self::Utf8(value.into_box_error())
86 }
87}
88
89impl From<io::Error> for ProtocolError {
90 fn from(value: io::Error) -> Self {
91 Self::Io(value)
92 }
93}
94
95impl fmt::Display for ProtocolError {
96 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
97 match self {
98 Self::Utf8(err) => write!(f, "UTF-8 error: {err:?}"),
99 Self::Io(err) => write!(f, "I/O error: {err:?}"),
100 Self::InvalidOpcode(code) => write!(f, "Encountered invalid opcode: {code}"),
101 Self::InvalidCloseSequence => write!(f, "Invalid close sequence"),
102 Self::MessageTooLong { size, max_size } => {
103 write!(f, "Message too long: {size} > {max_size}")
104 }
105 Self::UnmaskedFrameFromClient => {
106 write!(f, "Received an unmasked frame from client")
107 }
108 Self::WriteBufferFull(_) => write!(f, "Write buffer is full"),
109 Self::SendAfterClosing => {
110 write!(f, "Sending after closing is not allowed")
111 }
112 Self::ReceivedAfterClosing => {
113 write!(f, "Remote sent after having closed")
114 }
115 Self::NonZeroReservedBits => {
116 write!(f, "Reserved bits are non-zero")
117 }
118 Self::MaskedFrameFromServer => {
119 write!(f, "Received a masked frame from server")
120 }
121 Self::FragmentedControlFrame => {
122 write!(f, "Fragmented control frame")
123 }
124 Self::ControlFrameTooBig => {
125 write!(
126 f,
127 "Control frame too big (payload must be 125 bytes or less)"
128 )
129 }
130 Self::UnknownControlFrameType(t) => {
131 write!(f, "Unknown control frame type: {t}")
132 }
133 Self::ResetWithoutClosingHandshake => {
134 write!(f, "Connection reset without closing handshake")
135 }
136 Self::UnexpectedContinueFrame => {
137 write!(f, "Continue frame but nothing to continue")
138 }
139 Self::ExpectedFragment(data) => {
140 write!(f, "While waiting for more fragments received: {data}")
141 }
142 Self::UnknownDataFrameType(t) => {
143 write!(f, "Unknown data frame type: {t}")
144 }
145 Self::DeflateError(err) => write!(f, "Deflate error: {err:?}"),
146 }
147 }
148}
149
150impl error::Error for ProtocolError {
151 fn source(&self) -> Option<&(dyn error::Error + 'static)> {
152 match self {
153 Self::Utf8(err) | Self::DeflateError(err) => Some(err.as_ref()),
154 Self::Io(err) => Some(err as &(dyn std::error::Error + 'static)),
155 Self::InvalidOpcode(_)
156 | Self::InvalidCloseSequence
157 | Self::MessageTooLong { .. }
158 | Self::UnmaskedFrameFromClient
159 | Self::WriteBufferFull(_)
160 | Self::SendAfterClosing
161 | Self::ReceivedAfterClosing
162 | Self::NonZeroReservedBits
163 | Self::MaskedFrameFromServer
164 | Self::FragmentedControlFrame
165 | Self::ControlFrameTooBig
166 | Self::UnknownControlFrameType(_)
167 | Self::ResetWithoutClosingHandshake
168 | Self::UnexpectedContinueFrame
169 | Self::ExpectedFragment(_)
170 | Self::UnknownDataFrameType(_) => None,
171 }
172 }
173}