1+ //! Crate's error module.
2+ //!
3+ //! Contains all error types used throughout the crate.
4+
15use core:: convert:: Infallible ;
26
7+ /// Error decoding a WebSocket frame.
38#[ derive( Debug , thiserror:: Error ) ]
49pub enum FrameDecodeError {
10+ /// Reserved bits are not zero.
511 #[ error( "Reserved bits must be zero" ) ]
612 ReservedBitsNotZero ,
13+ /// Unmasked frame received from client.
14+ ///
715 /// The server must close the connection when an unmasked frame is received.
816 #[ error( "Received an unmasked frame from client" ) ]
917 UnmaskedFrameFromClient ,
18+ /// Masked frame received from server.
19+ ///
1020 /// The client must close the connection when a masked frame is received.
1121 #[ error( "Received a masked frame from server" ) ]
1222 MaskedFrameFromServer ,
23+ /// Invalid opcode.
1324 #[ error( "Invalid opcode" ) ]
1425 InvalidOpCode ,
15- // The payload length comes as a u64, converting it to usize might fail on 32-bit systems
26+ /// Payload length is too large.
27+ // XXX: The payload length comes as a u64, converting it to usize might fail on 32-bit systems
1628 #[ error( "Payload too large" ) ]
1729 PayloadTooLarge ,
30+ /// Control frame fragmented.
31+ ///
32+ /// Control frames must not be fragmented.
1833 #[ error( "Control frame fragmented" ) ]
1934 ControlFrameFragmented ,
35+ /// Control frame too large.
36+ ///
37+ /// Control frames must have a payload length of 125 bytes or less.
2038 #[ error( "Control frame too large" ) ]
2139 ControlFrameTooLarge ,
2240}
2341
42+ /// Error encoding a WebSocket frame.
2443#[ derive( Debug , thiserror:: Error ) ]
2544pub enum FrameEncodeError {
45+ /// Write buffer is too small to hold the encoded frame.
2646 #[ error( "Buffer too small" ) ]
2747 BufferTooSmall ,
2848}
2949
50+ /// Error decoding an HTTP request/response.
3051#[ derive( Debug , thiserror:: Error ) ]
3152pub enum HttpDecodeError {
53+ /// Error parsing the HTTP request/response.
3254 #[ error( "Parse error: {0}" ) ]
3355 Parse ( httparse:: Error ) ,
3456}
@@ -39,63 +61,83 @@ impl From<httparse::Error> for HttpDecodeError {
3961 }
4062}
4163
64+ /// Error encoding an HTTP request/response.
4265#[ derive( Debug , thiserror:: Error ) ]
4366pub enum HttpEncodeError {
67+ /// Write buffer is too small to hold the encoded HTTP request/response.
4468 #[ error( "Buffer too small" ) ]
4569 BufferTooSmall ,
4670}
4771
72+ /// Protocol specific errors/violations.
4873#[ derive( Debug , thiserror:: Error ) ]
4974pub enum ProtocolError {
75+ /// Close frame is invalid.
5076 #[ error( "Invalid close frame" ) ]
5177 InvalidCloseFrame ,
78+ /// Close code is invalid.
5279 #[ error( "Invalid close code" ) ]
5380 InvalidCloseCode ,
81+ /// Text message contains invalid UTF-8.
5482 #[ error( "Invalid UTF-8" ) ]
5583 InvalidUTF8 ,
84+ /// Fragment is invalid.
85+ ///
86+ /// This happens when a final fragment is received without any prior fragments.
5687 #[ error( "Invalid fragment" ) ]
5788 InvalidFragment ,
89+ /// Continuation frame is invalid.
90+ ///
91+ /// This happens when a continuation frame is received without an ongoing fragmented message.
5892 #[ error( "Invalid continuation frame" ) ]
5993 InvalidContinuationFrame ,
6094}
6195
96+ /// Error reading from a WebSocket connection.
6297#[ derive( Debug , thiserror:: Error ) ]
6398pub enum ReadError < I > {
99+ /// Error reading a WebSocket frame from the underlying I/O.
64100 #[ error( "Read frame error: {0}" ) ]
65101 ReadFrame (
66102 #[ source]
67103 #[ from]
68104 framez:: ReadError < I , FrameDecodeError > ,
69105 ) ,
106+ /// Error reading an HTTP request/response from the underlying I/O.
70107 #[ error( "Read http error: {0}" ) ]
71108 ReadHttp (
72109 #[ source]
73110 #[ from]
74111 framez:: ReadError < I , HttpDecodeError > ,
75112 ) ,
113+ /// Protocol error.
76114 #[ error( "Protocol error: {0}" ) ]
77115 Protocol (
78116 #[ source]
79117 #[ from]
80118 ProtocolError ,
81119 ) ,
120+ /// Fragments buffer is too small to read a frame.
82121 #[ error( "Fragments buffer too small to read a frame" ) ]
83122 FragmentsBufferTooSmall ,
84123}
85124
125+ /// Error writing to a WebSocket connection.
86126#[ derive( Debug , thiserror:: Error ) ]
87127pub enum WriteError < I > {
88128 /// Websocket connection is closed.
89129 ///
90- /// To close the TCP connection, you should drop the [`WebSocket`](crate::WebSocket) instance.
130+ /// To close the TCP connection, you should drop/close the underlying I/O instance.
91131 #[ error( "Connection closed" ) ]
92132 ConnectionClosed ,
133+ /// Error writing a WebSocket frame to the underlying I/O.
93134 #[ error( "Write frame error: {0}" ) ]
94135 WriteFrame (
95136 #[ source]
96137 #[ from]
97138 framez:: WriteError < I , FrameEncodeError > ,
98139 ) ,
140+ /// Error writing an HTTP request/response to the underlying I/O.
99141 #[ error( "Write http error: {0}" ) ]
100142 WriteHttp (
101143 #[ source]
@@ -104,6 +146,10 @@ pub enum WriteError<I> {
104146 ) ,
105147}
106148
149+ /// Error establishing a WebSocket handshake.
150+ ///
151+ /// # Generic Parameters
152+ /// `E`: User-defined error type for custom errors during the handshake.
107153#[ derive( Debug , thiserror:: Error ) ]
108154pub enum HandshakeError < E = Infallible > {
109155 /// Use of the wrong HTTP method (the WebSocket protocol requires the GET method to be used).
@@ -112,52 +158,75 @@ pub enum HandshakeError<E = Infallible> {
112158 /// Wrong HTTP version used (the WebSocket protocol requires version 1.1 or higher).
113159 #[ error( "HTTP version must be 1.1 or higher" ) ]
114160 WrongHttpVersion ,
161+ /// Connection was closed during the handshake.
115162 #[ error( "Connection closed during handshake" ) ]
116163 ConnectionClosed ,
164+ /// Invalid status code. (Should be 101 for switching protocols.)
117165 #[ error( "Invalid status code" ) ]
118166 InvalidStatusCode ,
167+ /// Missing or invalid (`Upgrade`: `websocket`) header.
119168 #[ error( "Missing or invalid upgrade header" ) ]
120169 MissingOrInvalidUpgrade ,
170+ /// Missing or invalid (`Connection`: `upgrade`) header.
121171 #[ error( "Missing or invalid connection header" ) ]
122172 MissingOrInvalidConnection ,
173+ /// Missing or invalid (`Sec-WebSocket-Accept`) header.
123174 #[ error( "Missing or invalid sec websocket accept header" ) ]
124175 MissingOrInvalidAccept ,
176+ /// Missing or invalid (`Sec-WebSocket-Version`) header.
125177 #[ error( "Missing or invalid sec websocket version header" ) ]
126178 MissingOrInvalidSecVersion ,
179+ /// Missing (`Sec-WebSocket-Key`) header.
127180 #[ error( "Missing sec websocket key header" ) ]
128181 MissingSecKey ,
182+ /// Other error.
183+ ///
184+ /// User-defined error type.
129185 #[ error( "Other: {0}" ) ]
130186 Other ( E ) ,
131187}
132188
189+ /// Fragmentation error.
133190#[ derive( Debug , thiserror:: Error ) ]
134191pub enum FragmentationError {
192+ /// Fragment size is zero.
135193 #[ error( "Fragment size must be greater than 0" ) ]
136194 InvalidFragmentSize ,
195+ /// Error indicating that a message type that cannot be fragmented was attempted to be fragmented.
196+ ///
197+ /// Only text and binary messages can be fragmented.
137198 #[ error( "Only text and binary messages can be fragmented" ) ]
138199 CanNotBeFragmented ,
139200}
140201
202+ /// General WebSocket error type.
203+ ///
204+ /// # Generic Parameters
205+ /// `E`: User-defined error type for custom errors during the handshake.
141206#[ derive( Debug , thiserror:: Error ) ]
142207pub enum Error < I , E = Infallible > {
208+ /// Error reading from the WebSocket connection.
143209 #[ error( "Read error: {0}" ) ]
144210 Read (
145211 #[ from]
146212 #[ source]
147213 ReadError < I > ,
148214 ) ,
215+ /// Error writing to the WebSocket connection.
149216 #[ error( "Write error: {0}" ) ]
150217 Write (
151218 #[ from]
152219 #[ source]
153220 WriteError < I > ,
154221 ) ,
222+ /// Handshake error.
155223 #[ error( "Handshake error: {0}" ) ]
156224 Handshake (
157225 #[ from]
158226 #[ source]
159227 HandshakeError < E > ,
160228 ) ,
229+ /// Fragmentation error.
161230 #[ error( "Fragment error: {0}" ) ]
162231 Fragmentation (
163232 #[ from]
0 commit comments